from random import randint
count = 0
Validation = False
def generateNumber():
Number = randint(1, 10)
return Number
def checkNumber(Guess):
Number = generateNumber()
if int(Guess) == Number and count < 1:
return "First time right! Genius?"
Validation = True
elif int(Guess) == Number and count == 2:
return "Correct. Second time, nice!"
Validation = True
elif int(Guess) == Number and count < 4:
return "Correct. Good job."
Validation = True
elif int(Guess) == Number and count > 3:
return "Correct. Took you long enough."
Validation = True
else:
return "Wrong! Try again "
while Validation == False:
Guess = input("Please guess a number between 1 to 10: ")
print (checkNumber(Guess))
count += 1
else:
TryAgain = input("Would you like to try again? y/n \n")
所以问题是,当用户猜到正确的号码时。验证应该转为True。所以while循环将停止循环。但是当玩家猜到正确的数字时,变量验证不会变为True。
答案 0 :(得分:0)
Python中的全局变量有点棘手,仅在文件顶部声明变量是不够的。
另外,在设置验证值之前,请保持返回值,导致函数在设置此变量之前终止
最后一个问题是while逻辑,请参阅使用raw_input而不是输入进行更正,否则缩进
只读对全局变量的访问按预期工作,但是当在函数中更改它们的值需要一个额外的步骤时,您应该将全局变量声明为全局变量。在你的情况下:
def generateNumber():
Number = randint(1, 10)
return Number
def checkNumber(Guess):
global Validation #<-- this instructs Python to use the global variable
Number = generateNumber()
if int(Guess) == Number and count < 1:
Validation = True
return "First time right! Genius?"
elif int(Guess) == Number and count == 2:
Validation = True
return "Correct. Second time, nice!"
elif int(Guess) == Number and count < 4:
Validation = True
return "Correct. Good job."
elif int(Guess) == Number and count > 3:
Validation = True
return "Correct. Took you long enough."
else:
return "Wrong! Try again "
while Validation == False:
Guess = input("Please guess a number between 1 to 10: ")
print (checkNumber(Guess))
count += 1
if (Validation):
if (raw_input("Would you like to try again? y/n \n") == "y"):
count = 0
Validation = False
else:
break
答案 1 :(得分:0)
在从方法返回之前,您需要将验证设置为 True ,如下所示:
if int(Guess) == Number and count < 1:
Validation = True
return "First time right! Genius?"
对所有if语句执行此操作。
答案 2 :(得分:0)
我在你的代码中发现了两个(一半)问题:
这是更新后的代码:
from random import randint
count = 0
Validation = False
def generateNumber():
Number = randint(1, 10)
return Number
def checkNumber(Guess):
global Validation
Number = generateNumber()
if int(Guess) == Number and count < 1:
Validation = True
return "First time right! Genius?"
elif int(Guess) == Number and count == 2:
Validation = True
return "Correct. Second time, nice!"
elif int(Guess) == Number and count < 4:
Validation = True
return "Correct. Good job."
elif int(Guess) == Number and count > 3:
Validation = True
return "Correct. Took you long enough."
else:
return "Wrong! Try again "
while Validation == False:
Guess = input("Please guess a number between 1 to 10: ")
print (checkNumber(Guess))
print "validation: %s" % str(Validation)
count += 1
TryAgain = input("Would you like to try again? y/n \n")
答案 3 :(得分:0)
除了全局变量问题,您还需要进行另一组修复。您对Validation = True的所有分配都出现在“return”语句之后。 Validation = True永远不会执行!它将在执行之前退出该函数。在“返回”之前设置“验证=真”。你仍然需要像Ishay所说的那样拥有全球声明。