问题描述:

b=int(1)

if b == 1:

b=2

c = "on my thumb"

elif b== 2:

b=3

c = "on my shoe"

elif b== 3:

b=4

c = "on my knee"

elif b== 4:

b+1

c = "on my door"

elif b== 5:

b+1

c = "on my hive"

elif b== 6:

b+1

c = "on my sticks"

elif b== 7:

b+1

c = "up in heaven"

elif b== 8:

b+1

c = "on my gate"

elif b== 9:

b+1

c = "on my spine"

else:

c = "once again"

for r in range(10):

print("This old man, he played one He played knick-knack " + c +" Knick-knack paddywhack, give your dog a bone This old man came rolling home")

b+1

Relatively new to coding so I don't really know what I am doing, but I am trying to get c to change every new time that r is printed....so that IT completes the old nursery rhyme. I am coding this in python...

网友答案:
parts = ["on my thumb", "on my shoe", ...]
numerators = ["one", "two", "three", ...]

for num, part in zip(numerators, parts):
    print "This old man, he played " + num
    print "He played knick-knack " + part
    print "Knick-knack paddywhack, give your dog a bone"
    print "This old man came rolling home"
网友答案:

Just store your strings in an array or list

lines = ["on my thumb", "on my shoe", "...."....]

and then iterate over that array (or list ) with the help of a for loop in c#-ish syntax:

foreach(line in lines)
{
  print("bla" + line + "blubb");
}
网友答案:

Maybe something like this:

string b[10] = {"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"};

for (i=0; i<b.length; i++) {
   print("This old man, he played one He played knick-knack " + b[i] +" Knick-knack paddywhack, give your dog a bone This old man came rolling home.\n");
}

Since those values will not change you can create a static array to hold the values and then you can cycle through the contents while displaying the result.

相关阅读:
Top