[英]How do I call a function which returns a variable, multiple times, while inside another function?
我有一個函數(基於文本的游戲),該函數在整個自身中要求多次輸入,在進行錯誤檢查之前,我想立即從中刪除所有空白。
為了減少冗余,我想到了使另一個函數同時執行這兩個操作,然后像這樣返回變量:
def startGame():
print("1, 2 or 3?")
response = response()
def response():
a = raw_input()
a = a.strip()
return a
startGame()
問題是我不斷得到:
UnboundLocalError:分配前已引用局部變量“ response”。
這對我來說毫無意義,因為為response分配了response()
的返回值。
我想念什么? 有沒有更簡單的方法可以做到這一點?
您指定的局部變量response
太 ; 您不能這樣做,它會掩蓋全局response()
函數。
重命名局部變量或函數:
def get_response():
# ...
response = get_response()
要么
def response():
# ....
received_response = response()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.