gpt4 book ai didi

python - Python 中的整数错误

转载 作者:行者123 更新时间:2023-11-28 19:40:33 25 4
gpt4 key购买 nike

所以我制作了一个非常简单的程序,从 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'

然后,当我将 lim -= 1 更改为 int(lim) -= 1 时,它显示 SyntaxError: illegal expression for augmented assignment

最佳答案

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

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

关于python - Python 中的整数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9644130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com