Skip navigation

Category Archives: Uncategorized

Well, I haven’t touched Python is almost 3 weeks 😦  On the plus side, I’ve been working on a website for a client, it’s done and online now.  Got more work from them so it’s all good 🙂

Screenshot-2

Looks life I need a new monitor…..dammit.

image

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

image

So last night i was trying to write my own code, as per the tutorial.  It was really hard at first.  Not necessarily the coding itself, although that was no walk in the park.  No, I was learning about making functions and after the main exercise they then said ”
Write at least one more function of your own design, and run it 10 different ways.”

This kinda threw me.  I mean, what the heck should I write?  What kind of function?  What focus should out be on?  Do i just write a mild variation of the tutorial one?  Give me some direction dammit!

Eventually I decided upon an inventory type of function.   I decided to combine some of my earlier lessons into it.  Namely the raw_input() and read() functions.

What I learned most, besides the syntax of the code, was understanding the differences between a string and an integer.  While I still don’t have a perfect understanding, I do have a better one than before and that is all that matters.

Here is the code I wrote.

# inventory counting function

from sys import argv

script, filename = argv

def inventory(desktop, laptop):
    print “You have %d desktops.” % desktop
    print “You have %d laptops.” % laptop
    print “You could start your own store! \n”

print “How many desktops and laptops did you count?”
desktops_counted = int(raw_input(“Desktops…”))
laptops_counted = int(raw_input(“Laptops…”))

inventory(desktops_counted, laptops_counted)

#need to read a file, then take numbers from that file and print them
print “How many desktops and laptops from last month?”
print “Read file %r:” % filename
print “File contained counts in a ‘desktops on line one,laptops on line two’ format.”
txt = open(filename)
line1 = int(txt.readline())
line2 = int(txt.readline())
inventory(line1,line2)