我想创建一个带有试用计数器的随机数生成器,但它说“无法分配给运营商”这里是我到目前为止
import random
number=random.randint(1,10)
print ("i am thinking of a number between 1 and 10")
counter=5
while counter>0:
if number==int(input("guess what the number is: ")):
print("well done")
else:
counter-1=counter #it displays it hear before the 1st counter
print ("your bad try again")
print ("it was" ,number,)
答案 0 :(得分:0)
可能是因为
counter-1=counter
但是你应该清楚地发布你的代码格式,以便于查看错误的位置!
答案 1 :(得分:0)
您无法修改变量名称:
import random
number = random.randint(1, 10)
print("i am thinking of a number between 1 and 10")
counter = 5
while counter > 0:
if number == int(input("guess what the number is: ")):
print("well done")
else:
counter = counter - 1 # you cant modify a variable name
print("your bad try again")
print("it was", number, )
输出:
i am thinking of a number between 1 and 10
guess what the number is: 5
your bad try again
guess what the number is: 4
your bad try again
guess what the number is: 3
your bad try again
guess what the number is: 2
your bad try again
guess what the number is: 1
your bad try again
it was 7
答案 2 :(得分:0)
您的代码问题在于如何减少counter
变量。
这是一个使用for
循环的更好解决方案。它会为你减少。
import random
number=random.randint(1,10)
print ("I am thinking of a number between 1 and 10")
print ("Guess what the number is - ",end='')
for __ in range(5):
if number==int(input()):
print("well done")
break
else:
print ("your bad try again")
print ("Number was " ,number)