Python中的整数错误

Bil*_*ljk 2 python math integer numbers

所以我做了一个非常简单的程序,从99开始倒数(唱99瓶啤酒),但我一直得到2个错误中的1个

#!/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'
Run Code Online (Sandbox Code Playgroud)

然后,当我改变lim -= 1int(lim) -= 1,它说SyntaxError: illegal expression for augmented assignment

Pea*_*oto 6

你需要将lim从字符串转换为整数.试试这个:

lim = int(input("What number do you want it to count down from?"))
Run Code Online (Sandbox Code Playgroud)

  • 如果你这样做,你也可以删除整个`sing()`中的所有其他转换. (6认同)