(Python初学者问题):::我想创建一个存储我的极限变量的变量,但是我只想在循环时递减它。就像我想为用户留下生命。
import random
limit = 5
i = 1
while i <= limit:
decrement_00 = limit #Problem1
decrement_00 = decrease - 1
num = random.randint(1,6)
user = int(input("enter a number: "))
if user == num:
print("You're correct")
break
else:
print("Try again.")
print(f"You only have {decrement_00} left.")
i = i + 1
答案 0 :(得分:1)
解决方案:如果要显示其余的迭代次数(限制),则可以简单地打印(limit - i
,而不使用其他变量。
现在您的代码中出现问题了,您正在打印decrement_00
以显示给用户,只需查看以下两行,您就会了解错误。
decrement_00 = limit #Problem1
decrement_00 = decrease - 1
提示:
是否已初始化减少?
decrement_00的值会改变吗?
答案 1 :(得分:1)
尝试一下:
import random
limit = 5
i = 1
while i <= limit:
#decrement_00 = limit #Problem1
limit = limit - 1
num = random.randint(1,6)
user = int(input("enter a number: "))
if user == num:
print("You're correct")
break
else:
print("Try again.")
print(f"You only have {limit} left.")
#i = i + 1