2

I'm doing the old 99 bottles song and trying to do it using a While loop, to help me continue to better learn that loop type.

I'm wondering why I would be getting a TypeError when in my code below and what arguments exactly I'm missing?

Here is my code:

# Get number of beers
bottles = int(raw_input("How many bottles of beer? "))

# return invalid response
if bottles < 1:
    print "That's not a good number"

    if bottles == 1:
        s1 = "bottle" 
        s2 = "bottles" 

    elif bottles == 2:
        s1 = "bottles" 
        s2 = "bottles" 

# sing verses
while bottles > 0:
    print "%d %s of beer on the wall," % bottles, s1
    print "%d %s of beer." % bottles, s1
    print "You take one down, pass it around,"
    print "%d %s of beer on the wall." % (bottles - 1), s2
    print
    bottles -= 1

And here is the error:

Traceback (most recent call last):
    File "beer.py", line 47, in <module>
          print "%d %s of beer on the wall," % bottles, s1
TypeError: not enough arguments for format string

I've tried using parenthesis around the "bottles, s1" after the %, but still doesn't help. Any suggestions?

5
  • Using (bottles, s1) works for me. Commented Dec 15, 2012 at 22:02
  • What if the input for bottles is 10. Where does s1 and s2 get defined? Commented Dec 15, 2012 at 22:03
  • Your code will generate a NameError if you set bottles to anything larger than 2, since in that case neither of your if/elif will execute. You should change the elif to an if. Also it looks like you have an indentation problem. The if/elif should not be under the if bottles < 1. Commented Dec 15, 2012 at 22:04
  • And it also would never update your s1 and s2 strings since they only get evaluated once from the user input. The while loop will just spin until it'd done. You would need to move them inside of the while loop so they get set properly each time. Commented Dec 15, 2012 at 22:05
  • I guess I'm still just confused as to how I should be defining my conditions for s1 and s2 and then how they're supposed to be called on in the song. Commented Dec 15, 2012 at 22:56

1 Answer 1

5

you have to specify multiple arguments as tuple, eg

print "%d %s of beer on the wall," % (bottles, s1)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help. Along with specifying those arguments as a tuple, I also realized that 1.) the While loop was in the wrong place, should have been below the corner case 2.) I didn't have an else clause to allow for numbers higher than 2. Cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.