【发布时间】:2019-09-28 02:29:41
【问题描述】:
我正在学习使用 python 编程,但遇到了这个问题:我正在尝试制作一个猜谜游戏,并且在尝试检查获胜条件时,该函数无法识别输入变量,我确保我返回了以前的函数。所以我得到 'name > 错误。我认为这与变量不是全局的或类似的东西有关。
import random
ran_int = random.randint(1,100)
guesses = 0
# here you input the number and it keeps asking unless you do so with 1 to 100
def ask():
first_input = 0
while first_input < 1 or first_input > 100:
first_input = int(input('Enter a number between 1 and 100: '))
return first_input
# this is just to increment the number of guesses stored for showing at the end # of the game
def guesses_inc():
global guesses
guesses += 1
return guesses
# here is where i get the error, as if my ask() function didn't return
# the value properly or as if I assigned it wrongly
def check_win_1():
if first_input == ran_int:
guesses_inc()
print(f'BINGO!\nYou guessed correctly after {guesses} times.')
elif (abs(ran_int - first_input) <= 10):
guesses_inc()
print('WARM!')
ask2()
elif first_input < 1 or first_input > 100:
print('Out of bounds!')
ask2()
else:
guesses_inc()
print('COLD!')
ask2()
ask()
check_win_1()
这是错误
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-11-bfd5497995df> in <module>
----> 1 check_win_1()
NameError: name 'first_input' is not defined
我没有粘贴整个代码,因为在测试它时它在这个阶段返回了错误,所以我认为其余的对于这个特定的问题并不重要。我尝试将 var 输入设为全局和类似的东西,但我认为我做得不对。
【问题讨论】:
-
你可能在最后一行
check_win_1后面缺少括号 -
你还没有定义
ask2()方法
标签: python