這是我正在撰寫的一個簡單的基于文本的游戲的代碼片段,以提高我的 Python 技能。我計劃用它input_check()來簡化我稍后將在專案中撰寫的大量代碼,但目前我無法讓它作業。我正在使用 Pylance 擴展的最新 VS Code 大師上運行它,它不會標記我的代碼中的任何錯誤。我已經運行了多個測驗以確保input_check()是問題所在,洗掉它并簡單地多次運行代碼就可以了。
import time
def rules():
print("The rules of this game are:")
time.sleep(0.5)
print("rules")
time.sleep(0.5)
print("rules")
time.sleep(0.5)
print("rules")
time.sleep(0.5)
print("rules")
time.sleep(0.5)
print("rules")
time.sleep(0.5)
input_check("Do you understand?\n", rules(), "Ok. Starting game...")
def input_check(question: str, function, confirmation: str):
yes_no = input(question)
if yes_no.lower() == "n" or "no":
function
elif yes_no.lower() == "y" or "yes":
print(confirmation)
time.sleep(1)
else:
print("Invalid input.")
input_check(question, function, confirmation)
input_check("Do you know the rules?\n", rules(), "Ok. Starting game...")
我幾乎是 Python 的完全新手,所以我不知道是否接受function引數然后將其作為函式運行input_check()是否可行,但這不是問題。
應該有一個提示運行來定義yes_no,input()但它永遠不會達到這個。相反,它似乎向前跳到運行rules()(只有當用戶輸入“否”或“n”時才會發生這種情況),并rules()連續運行直到停止,完全跳過input_check()。
我的問題是:
- 為什么會
input_check()被完全忽視? - 您可以按原樣(引數)運行作為引數的代碼,
function還是需要額外的步驟才能使其運行? - 有沒有更好/更有效的方法來做到這一點?(例如,一個決議輸入的包,避免了你自己的函式)
uj5u.com熱心網友回復:
看看這個宣告:
input_check("Do you know the rules?\n", rules(), "Ok. Starting game...")
當您這樣做時,Python 將立即呼叫該rules函式,因此它可以將其結果傳遞給input_check. 您的rules函式列印出一堆東西,然后有完全相同的行,它將一次又一次地呼叫rules(),一次又一次,一次又一次......它永遠沒有機會呼叫input_check. 它仍在處理引數。
如果您想傳遞函式物件但不呼叫它,請不要使用括號:
input_check("Do you know the rules?\n", rules, "Ok. Starting game...")
請注意,該input_check函式將繼續呼叫傳入的函式。你不需要在里面再次呼叫它rules。
跟進
這不符合您的想法:
if yes_no.lower() == "n" or "no":
這被決議為:
if (yes_no.lower() == "n") or "no":
并且由于“否”是正確的,因此將始終采用 if。您應該使用以下之一:
if yes_no.lower() in ("n" or "no"):
if yes_no.lower()[0] == "n":
接下來,你有這個:
if yes_no.lower() == "n" or "no":
function
在這里,您確實想呼叫該函式,因此您需要添加括號:
if yes_no.lower()[0] == "n":
function()
uj5u.com熱心網友回復:
inputCheck("Do you know the rules?\n", rules(), "Ok. Starting game...")
您不需要任何 parantheses() 在規則之后,而不是將函式作為引數傳遞給您正在運行它。像這樣寫:-
inputCheck("Do you know the rules?\n", rules, "Ok. Starting game...")
也在這里:-
if yes_no.lower() == "n" or "no":
function
您需要在函式后添加 (),撰寫:-
if yes_no.lower() == "n" or "no":
function()
讓我知道它是否能解決問題
uj5u.com熱心網友回復:
- 問題是您
rules()像引數一樣使用來傳遞函式。您需要更改為:inputCheck("Do you know the rules?\n", rules, "Ok. Starting game...")。
rules():將呼叫函式 rules()
規則:函式可以作為引數傳遞給另一個函式。
您可以來這里獲取更多資訊:
在python中呼叫帶括號和不帶括號的函式有什么區別?.
注意:我看到您的示例代碼有很多錯誤(使用def rules()物件或函式時)。您應該學習如何除錯,它將幫助您有效地修復錯誤
uj5u.com熱心網友回復:
連同其他答案,我發現了另一個語意錯誤:您的第一個 if 陳述句將始終評估為真,因為它將評估“否”的布林值,如下所示
if yes_no.lower() == 'n' or 'no' == True:
由于非空字串的計算結果為真,因此該陳述句將始終執行。而不是你所擁有的,你可以添加
yes_no.lower() == 'no'
給你
if yes_no.lower() == "n" or yes_no.lower() == "no":
使該陳述句僅在 yes_no.lower 為“n”或“no”時才評估為真
如需進一步說明,請參閱
為什么我的 python if 陳述句不起作用?
uj5u.com熱心網友回復:
你得到了很多關于代碼當前行為的解釋,但沒有太多關于如何做我認為你正在嘗試做的事情的實用建議。您不需要來回傳遞規則函式。您需要最重要的工具來獲取用戶輸入:while-true 回圈。
def game():
if not yesno('Do you know the rules'):
rules()
print("Ok. Starting game ...")
def rules():
while True:
print("The rules of this game are ... BLAH BLAH")
if yesno('Do you understand'):
break
def yesno(question):
while True:
yn = input(f'{question}? [yes/no]: ').lower()
if yn in ('y', 'yes'):
return True
elif yn in ('n', 'no'):
return False
else:
print("Invalid input.")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/448930.html
