Skip navigation

Taking this course is teaching me to balance doing the practice exercises and when to start the mini-project that will be graded.  At some point you will be doing the practice exercises and will get stuck.  Now what?  Do you spend tons of time trying to get help/Google for a solution or after two days do you give up and simply wish you had spent that same time working on your project because you know you WILL get stuck at some point on that.  Choices, choices, choices.

Here is a practice exercise that I finished and it wasn’t too hard to figure out but glad I did because it was important towards my ‘Timer’ game (which I’ll post after the deadline goes by).

# Define a function that returns formatted minutes and seconds

#import simplegui

# Globals

###################################################
# Circle area formula
# Student should enter function on the next lines.
# Helper Functions
def format_time(time):
#split into minutes and seconds
minutes = int(time/60)
seconds = time % 60

#convert to strings
minutes_string = str(minutes)
seconds_string = str(seconds)
# print minutes_string + ” minutes”
# print seconds_string + ” seconds”

# return composite string
if minutes == 0 and seconds == 0:
return “0 Seconds and 0 Minutes \n”
elif minutes == 0:
return “0 Minutes ” + seconds_string + ” Seconds \n”
elif seconds == 0:
return minutes_string + ” Minutes” + ” 0 Seconds \n”
else:
return minutes_string + ” Minutes” + ” and ” + seconds_string +” Seconds \n”

###################################################
# Tests

print format_time(23)
print format_time(1237)
print format_time(0)
print format_time(1860)

###################################################
# Output to console
#0 minutes and 23 seconds
#20 minutes and 37 seconds
#0 minutes and 0 seconds

#31 minutes and 0 seconds

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: