Natasha Kenneley
Natasha Kenneley

Reputation: 1

Stuck implementing "man went to mow" rhyme in Python for my homework

We have to write a program that will be like the kids rhyme one man went to mow. We have to do this using lists. My teacher isn't so worried about the correct english of man and men.

This is a link of the rhyme http://www.kididdles.com/lyrics/o105.html

This is what I have so far...

men = input ('enter how many men you would like to mow the meadow')
menmow = 1
menlist = []
while menmow <men:
   print str(menmow) + ' man went to mow'
   print 'went to mow a meadow'
   print 'one man and his dog'
   print 'went to mow a meadow'
   menlist.insert [0.2]
if menmow >men:
   print 'your meadow has been mowed'

Upvotes: 0

Views: 653

Answers (6)

Kneel-Before-ZOD
Kneel-Before-ZOD

Reputation: 4221

I saw the problem, and decided to try it, just for the heck. Here's what I came up with.
Since the user input has to be digits, you need to create a dictionary to map the digits to numbers (in words, e.g. 1 becomes one). And in order to create a dictionary, you need to know the maximum value a user will enter.
OR
Use the digits just like that, instead of the numbers in words.
I chose the second option; here's the code; feel free to mess with it.

def one_man():
    return """
            One man went to mow,\n
            Went to mow a meadow, \n
            One man and his dog, \n
            Went to mow a meadow.\n

      """;

def many_men(num_of_men, collated_men): return """ {0} men went to mow,\n Went to mow a meadow, \n {1} 1 man and his dog, \n Went to mow a meadow.\n """ . format(num_of_men, collated_men);

def collate_men(num_of_men): result = ""; while num_of_men > 1: result += "{0} men, " . format(num_of_men); num_of_men -= 1; return result;

if name == "main": preamble = """ To exit this program, type y or n when prompted"""; print preamble;

loop = True; while loop: preamble = raw_input("""Want to exit the program? y/n : """); if preamble == "y": loop = False; break; else: num_of_men_value = raw_input("""Enter the number of men you would like to mow the meadow with?: """); try: num_of_men = int(num_of_men_value); if num_of_men < 1: print "Digit must be greater than 0"; elif num_of_men == 1: print one_man(); else: collated_men = collate_men(num_of_men); print many_men(num_of_men, collated_men); continue; except: print "Number must be a valid digit"; continue;

Upvotes: 0

Blender
Blender

Reputation: 298364

  • You can simplify your while loop by just using a for loop and a range() object.
  • Use functions. They are your friends.
  • Join the contents of a list together using ', '.join(your_list)

Here's how I'd tackle the problem:

def lyrics(men):
  verses = []

  verses.append(str(len(men)) + ' men went to mow,')
  verses.append('Went to mow a meadow,')
  verses.append(', '.join(reversed(men)) + ' and his dog,')

  return '\n'.join(verses)

num_men = int(raw_input('How many men will mow the meadow? '))

song = []
men = []

for man in range(1, num_men + 1):
  men.append(str(man) + ' men')
  song.append(lyrics(men))

print '\n\n'.join(song)
print
print 'your meadow has been mowed'

To actually make your code spit out the real song (with spelled-out numbers), use a dictionary to map numbers to words:

num_men = int(raw_input('How many men will mow the meadow? '))

def lyrics(men):
    return '{num_men} went to mow,\nWent to mow a meadow,\n{men} and his dog'.format(
      num_men=len(men),
      men=', '.join(reversed(men)).capitalize()
  )

song = []
men = []

numbers = {
    1: 'one',
    2: 'two',
    3: 'three',
    4: 'four',
    5: 'five',
    6: 'six',
    7: 'seven',
    8: 'eight',
    9: 'nine'
}

for man in range(1, num_men + 1):
    men.append('{} {}'.format(numbers[man], 'man' if man == 1 else 'men'))
    song.append(lyrics(men))

print '\n\n'.join(song)
print
print 'your meadow has been mowed'

Upvotes: 0

Hans Then
Hans Then

Reputation: 11322

You are quite far like this. Some details are wrong, which you will discover while testing. What is missing is the correct implementation of the line "one man and his dog". This should change for each couplet.

What you can do is create a second loop to output One man, Two man etc. Hint: print "text", will print text, but supress the newline.

As an alternative, you can use a for loop to build up the text as a list, and then output that. Like in your main loop you can keep a counter and append str(counter) + " man" to the list. To output this list as a string, use ' '.join(list).

Upvotes: 1

nneonneo
nneonneo

Reputation: 179552

menlist.insert[0.2] is invalid; you will end up with a cryptic error like 'builtin_function_or_method' object has no attribute '__getitem__'.

As insert is a function, use the function call syntax: menlist.insert(0, 2) to insert 2 at the start of the list (at index 0).

Upvotes: 0

Makoto
Makoto

Reputation: 106460

If you're using Python 2.x, then the input() function will execute the statement as if it were Python code. That's probably not what you want. Also, you attempt to do menmow < men, which will be an invalid comparison between an int and a String.

Change your input statement to this:

men = int(raw_input ('enter how many men you would like to mow the meadow'))

You will also need a statement to end your loop. It will run indefinitely, since the loop condition doesn't care about menlist.

Lastly, menlist.insert [0.2] isn't valid syntax. Be sure to check on how you insert values into a list.

Upvotes: 0

mpenkov
mpenkov

Reputation: 21906

For starters, you have an infinite loop (the while loop will never terminate).

Upvotes: 1

Related Questions