Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer Errors in Python

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

like image 778
Billjk Avatar asked Dec 12 '25 01:12

Billjk


2 Answers

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?"))
like image 85
PearsonArtPhoto Avatar answered Dec 14 '25 20:12

PearsonArtPhoto


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.

like image 38
Makoto Avatar answered Dec 14 '25 20:12

Makoto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!