so my teacher made us do this 'one man went to mow' nursery rhyme in python, and I just can't seem to figure out how to make it work.
num = ['One man','Two men','Three men','Four men','Five men',
'Six men','Seven men','Eight men','Nine men','Ten men']
for i in range(0,len(num)):
print(num[i],'went to mow, went to mow a meadow,')
# Check if i is greater than 0, to avoid index out of range errors
if i > 0:
# Loop through the previous elements in the num list
for n in range(i-1,-1,-1):
print(num[i],num[n],'and his dog, went to mow a meadow.')
The objective is to try get it to print:
'One man went to mow, went to mow a meadow One man and his dog Went to mow a meadow
Two men went to mow Went to mow a meadow Two men, one man and and his dog Went to mow a meadow
Three men went to mow Went to mow a meadow Three men, two men, one man And his dog Went to mow a meadow
Four men went to mow Went to mow a meadow Four men, three men, two men, one man And his dog Went to mow a meadow'
basically all the way up to the 10th one. the bit im stuck on is getting it count down like "Four men, three men, two men, one man". been stuck on this for a while and would appreciate some help
Tried fixing it many different ways like changing the nested for loop to i-1, to len(nums) and more, but never worked.
CodePudding user response:
Looping with indices is not usually necessary in Python. Try this:
for i, men in enumerate (num):
menlist = ' '.join(num[i : : -1])
print (men, ' went to mow, went to mow a meadow ', menlist, 'and his dog went to move a meadow')
CodePudding user response:
A different approach is to try to define the structure, capitalization and pluralization in distinct functional bits, instead of a hardcoded list of partial phrases. This makes for slightly more generalizable code. Later, you could even import and use inflect
for larger numbers (and for pluralization of other words too, if needed).
Edit: defining a line(n)
function:
num = 'one two three four five six seven eight nine ten'.split()
trailing = ' went to mow a meadow.'
def mm(n):
return 'man' if n < 2 else 'men'
def line(n):
i = n - 1
part1 = f'{num[i]} {mm(i 1)} went to mow,{trailing}'.capitalize()
part2 = ', '.join(reversed(
[f'{word} {mm(j 1)}{" and his dog" if j==0 else ""}'
for j, word in enumerate(num[:i 1])])).capitalize()
part2 = trailing
line = ' '.join([part1, part2])
return line
Example:
>>> line(3)
'Three men went to mow, went to mow a meadow. Three men, two men, one man and his dog went to mow a meadow.'
To produce the whole nursery rhyme:
for n in range(1, len(num) 1):
print(line(n))
Produces:
One man went to mow, went to mow a meadow. One man and his dog went to mow a meadow.
Two men went to mow, went to mow a meadow. Two men, one man and his dog went to mow a meadow.
Three men went to mow, went to mow a meadow. Three men, two men, one man and his dog went to mow a meadow.
...
Ten men went to mow, went to mow a meadow. Ten men, nine men, eight men, seven men, six men, five men, four men, three men, two men, one man and his dog went to mow a meadow.