views:

195

answers:

8

I'm a beginner in python programming and on one of my assignments I am to use python to write a code that will say these lyrics.

This old man, he played one
He played knick-knack on my thumb
Knick-knack paddywhack, give your dog a bone
This old man came rolling home

This old man, he played two
He played knick-knack on my shoe
Knick-knack paddywhack, give your dog a bone
This old man came rolling home

This old man, he played three
He played knick-knack on my knee
Knick-knack paddywhack, give your dog a bone
This old man came rolling home

This old man, he played four
He played knick-knack on my door
Knick-knack paddywhack, give your dog a bone
This old man came rolling home

This old man, he played five
He played knick-knack on my hive
Knick-knack paddywhack, give your dog a bone
This old man came rolling home

This old man, he played six
He played knick-knack on my sticks
Knick-knack paddywhack, give your dog a bone
This old man came rolling home

This old man, he played seven
He played knick-knack up in heaven
Knick-knack paddywhack, give your dog a bone
This old man came rolling home

This old man, he played eight
He played knick-knack on my gate
Knick-knack paddywhack, give your dog a bone
This old man came rolling home

This old man, he played nine
He played knick-knack on my spine
Knick-knack paddywhack, give your dog a bone
This old man came rolling home

This old man, he played ten
He played knick-knack once again
Knick-knack paddywhack, give your dog a bone
This old man came rolling home

This is what I have so far, but it doesn't do what I want it to do. I'm not sure how to phrase the while loop or get it to choose a single word from the list in order.

num = ['one','two','three','four','five','six','nine','ten']
end = ['on my thumb','on my shoe','on my knee','on my door','on my hive','on my sticks','up in heaven','on my gate','on my spine','once again']
z=1

print "This old man, he played",(num)
print "He played knick-knack", (end)
print "Knick-knack paddywhack, give your dog a bone"
print "This old man came rolling home"
+5  A: 

Hint: You will need to use a loop

I would look at the Python Flow Control documentation. Also note the range() function.

You can grab the n'th element from an array like this:

val = some_array[n]

And remember that in Python, arrays start counting at 0, not 1.

Adam Batkin
A: 

I am not sure if I want to do your homework, but I will give you some hints: To me, this looks like a good place for a for loop. Use range, xrange, or enumerate.

mikez302
A: 

First, you probably want to say z = 0 because arrays in Python (and most programming languages) are zero-indexed - the first element of num is num[0], the second is num[1], and so on. Though we don't really even need z. More on that later.

Second, I would change num and end to nums and endings. If we give them a plural name it's clearer that we're using a list. Also, lists aren't individual values, but a collection of values. We need to get an element of that collection in this case. We wouldn't say (nums) (or (num) if you keep it your way) - that gets the entire list. We would say nums[x] (or num[x]) which gets element x (remember that arrays are zero-indexed!), where x can be a number, a variable holding a number, or any arbitrary expression that evaluates to a number.

Third, you could use a while loop, but even better would be a for loop and the range() and len() functions. The syntax for for loops is:

for x in y:

Where x is a temporary variable, and y is a list of items. The loop iterates over all the items in y, setting each one to x for the body of the loop. The range() function creates a list of numbers (which can be easily used as array indices, hint hint). The len() function takes a list and returns the length of the list. Combine these in the appropriate order to create a loop.

Chris Lutz
Just a final note, this isn't so much homework because it isn't for a grade. For some reason there is no grading in this class because it is an intro class. I'm just trying to get used to the programming. So do I have it set up right for the most part?
Josh
The reason we're not writing the code for you isn't because it's for a grade, it's because we want you to learn it. In this case, I can see you learning from an example, but I suggest you look up the `range()` function (all you need to know is the 1-argument form for this case). If you look at that and can't figure it out, I'll give you another nudge in the right direction.
Chris Lutz
Yeah, wasn't expecting/wanting someone to write the code for me, just trying to figure out some help. I'm looking at the range() function and trying to figure out how to write it the way I need to
Josh
If we have a list `nums` we get the length of that list with `len(nums)` and we can get a list of array indices for that list with `range(len(nums))` and we can iterate over the list of indices with the `for` loop shown in my answer. Is it starting to make sense?
Chris Lutz
It is starting to, while looking at the guy's answer above yours, I would still like to learn how to do this on my own. This is my second week using python and If I'm going to have this as a profession I might as well learn it. I'm trying to read these guides and while some of it might make sense, I'm not sure how to apply it.
Josh
A: 

Your rhyme encourages violence against the Irish. Nevertheless, here's some help.

Since the rhymes themselves have two parts, I've put each rhyme as a two part list, put together into a list of all those rhymes.

# List of rhymes, with each part of the rhyme as another list.
rhymes = ['one','on my thumb'],['two','on my shoe']

# The total amount of rhymes len(rhymes) lets us know when to stop...
count = 0
while count < len(rhymes):
    print "This old man, he played "+rhymes[count][0]
    print "He played knick-knack "+rhymes[count][1]
    print "Knick-knack paddywhack, give your dog a bone"
    print "This old man came rolling home \n"
    count += 1
nailer
I would a) use a `for` loop, b) use `,` instead of `+` c) probably use `zip()` to build the combined list for me rather than making it myself, but otherwise you've done a good job on his homework. :P
Chris Lutz
I think it is better and more Pythonic to directly loop over the things you need, instead of deriving a length and then indexing to find the things you need.
steveha
I'm with steveha, and also destructuring the tuple to get the values is more pythonic than accessing by index.
fortran
Alas, using a for loop doesn't output the elements in order.
nailer
A: 

first half shamelessly copied from from nailer's answer:

# List of rhymes, with each part of the rhyme as another list.
rhymes = ['one','on my thumb'],['two','on my shoe']

for number, position in rhymes:
    # print your output in terms of number and position
Jimmy
+2  A: 

There is in fact a more Pythonic answer to your homework. Again, I can't give it to you directly, but as well as looking at the loop documentation, you might also want to look at zip. You can do a lot in Python without directly using index variables.

Kathy Van Stone
+2  A: 

First, read up on tuples and lists in Python; then read up on the for loop.

I suggest that you use a tuple to store things that go together.

# define a list of tuples
lst = [ ("eggs", "an omelet"), ("bread", "a sandwich"), ("sugar", "cookies") ]

for ingredient, food in lst:
    print "I need", ingredient, "to make", food + "."

If you run the above code, here is the output you will get:

I need eggs to make an omelet.
I need bread to make a sandwich.
I need sugar to make cookies.

This is the Pythonic way to solve this problem. Here is another way, which I don't like as well:

ingredients = ["eggs", "bread", "sugar"]
foods = ["an omelet", "a sandwich", "cookies"]

for i in range(len(ingredients)):
    print "I need", ingredients[i], "to make", foods[i] + "."

This will print the same output as the previous example, but it's harder to work with. You need to make sure that the two lists stay synchronized. The whole "list of tuples" thing may seem weird, but it's actually much easier once you are used to it.

I suggest you get the book Learning Python and study that; it will teach you a lot and it is very clear.

steveha
A: 

I appreciate all the help, I was able to figure out my own working code. All of you have been very helpful. I will be sure to use this site for help in the future.

Josh