Jos*_*ber 0 python user-input return function
我有一个函数(基于文本的游戏),它在整个过程中多次请求输入,在进行错误检查之前,我想要立即删除所有空格.
为了减少冗余,我想做另外两个函数,然后像这样返回变量:
def startGame():
print("1, 2 or 3?")
response = response()
def response():
a = raw_input()
a = a.strip()
return a
startGame()
Run Code Online (Sandbox Code Playgroud)
问题是我不断得到:
UnboundLocalError:赋值前引用的局部变量'response'.
这对我没有意义,因为响应被赋予了response()返回值.
我错过了什么?有更简单的方法吗?
您指定的局部变量response 太多 ; 你不能这样做,它掩盖了全局response()功能.
重命名局部变量或函数:
def get_response():
# ...
response = get_response()
Run Code Online (Sandbox Code Playgroud)
要么
def response():
# ....
received_response = response()
Run Code Online (Sandbox Code Playgroud)