2

So I made a very simple program that counts down from 99 (sings 99 bottles of beer) but I keep getting 1 of 2 errors

#!/usr/bin/env python
print("This program sings the song 99 bottles of beer on the wall")
lim = input("What number do you want it to count down from?")
def sing():
    global lim
    while int(lim) >= 0:
        if int(lim) != 1 or int(lim) != 0:
            print(lim, "bottles of beer on the wall", lim, "bottles of beer")
            print("Take one down pass it around...")
            print(lim, "bottles of beer on the wall")
            input("\nPRESS ENTER\n")
            lim -= 1
sing()
TypeError: unsupported operand type(s) for -=: 'str' and 'int'

Then, when I change lim -= 1 to int(lim) -= 1, it says SyntaxError: illegal expression for augmented assignment

2
  • Consider: for bottles in range(lim, 0, -1): .... Commented Mar 10, 2012 at 5:24
  • Are you on Python 2 or 3? Because input() and the print() function are used in Python 3, but your first script line reads: #!/usr/bin/env python instead of #!/usr/bin/env python3. Could you specify which one are you trying to run? Commented Mar 10, 2012 at 5:50

3 Answers 3

6

You need to covert lim from a string to an integer. Try this:

lim = int(input("What number do you want it to count down from?"))
Sign up to request clarification or add additional context in comments.

1 Comment

If you do this, you can also remove all of the other conversions peppered throughout sing().
4

If you're using Python 2.x (you don't specify), use raw_input instead.

lim = int(raw_input("What number do you want it to count down from?"))

From there, you can remove all the checks to int(lim), as lim is already an integer.

1 Comment

The first line of the OP code reads: #!/usr/bin/env python, but after print is used like a function (who knows, maybe it's from the __future__).
3

You get that TypeError because lim is a string. And strings do not support a -= operator:

>>> s = '10'
>>> s -= 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -=: 'str' and 'int'

What you need to do is to convert lim to integer, something like this:

lim = input('Insert number: ')
lim = int(lim)

Don't worry about print after, it can print integers too, not just strings :)


I'd also say that there's a main problem with your first line. Judging from your code, this:

#!/usr/bin/env python

should be

#!/usr/bin/env python3

since you're writing code with Python 3 syntax/fashion.

You could also get rid of the global statement:

#!/usr/bin/env python3
def sing():
    print("This program sings the song 99 bottles of beer on the wall")
    lim = input("What number do you want it to count down from?")
    lim = int(lim)
    while lim > 1:
        print(lim, "bottles of beer on the wall", lim, "bottles of beer")
        print("Take one down pass it around...")
        print(lim, "bottles of beer on the wall")
        input("\nPRESS ENTER\n")
        lim -= 1
sing()

2 Comments

input() is perfectly valid syntax in Python 2.x, it just doesn't do what raw_input does. input() evaluates the code in 2.x; in 3.x it merely reads it in.
@Makoto: Yes, but not in the way the OP use it. There would be no need for converting to integer after.

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.