这里是完整的脚本:
import random
aNumber=random.randrange(100)+1
print("I am thinking of a number 1 through 100.")
if aNumber>50:
print("It has a value greater than 50.")
else:
print("It has a value less than or equal to 50.")
while True:
answer=int(raw_input("What is your guess?\n"))
count+=1
if aNumber==answer:
print("Congratulations, you have guessed the number!")
elif aNumber<answer:
print("Try a lower number")
elif aNumber>answer:
print("Try a higher number")
这是我收到的错误消息:
line 11, in <module>
count+=1
NameError: name 'count' is not defined
答案 0 :(得分:3)
您需要在通过 count
运算符增加其值之前定义 += 1
。实际上,count += 1
等价于 count = count + 1
,因此必须在之前定义计数(例如:count = 0
在脚本的开头)。
答案 1 :(得分:0)
Count 未在 GetStreamAsync
之前定义,您可能应该像这样在 while 循环之上声明它:
count += 1
答案 2 :(得分:0)
如果我没记错的话,这行得通。
import random
aNumber = random.randrange(100)+1
print("I am thinking of a number 1 through 100.")
if aNumber>50:
print("It has a value greater than 50.")
else:
print("It has a value less than or equal to 50.")
count = 0 # count is a variable name and it has to be defined beforehand of using
while True:
answer=int(input("What is your guess? \n"))
count+=1
if aNumber == answer:
print("Congratulations, you have guessed the number!")
elif aNumber < answer:
print("Try a lower number")
else:
print("Try a higher number") # since this is the remaining condition, you can simply use else