Skip navigation

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)

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: