Python input() 在函数调用期间没有被调用
这是我为提高 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()
中将其作为函数运行是否可行,但这不是问题。
应该有一个提示运行来使用 input()
定义 yes_no
但它永远不会达到此目的。相反,它似乎跳到运行 rules()
(只有当用户输入“no”或“n”时才会发生),并且 rules()
运行连续直到停止,完全跳过 input_check()
。
我的问题是:
- 为什么
input_check()
被完全忽略? - 您可以按原样运行作为参数的代码(
function
参数),还是需要执行额外的步骤才能使其运行? - 有没有更好/更有效的方法来做到这一点? (例如,解析输入的包,避免必须创建自己的函数)
This is a code snippet from a simple text based game I'm writing to improve my Python skills.
I plan on using input_check()
to simplify a lot of code that I'll write later on in the project, but I can't get it to work at the moment. I'm running this on the latest master of VS Code with the Pylance extension, which doesn't flag any errors in my code. I've run multiple tests to make sure input_check()
is the issue, and removing it and simply running the code multiple times works just fine.
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...")
I'm almost a complete newbie to Python so I have no idea if taking the function
argument and then running it as a function later in input_check()
would work or not, but that's not the issue.
There should be a prompt run to define yes_no
with input()
but it never reaches this. Instead, it seems like it skips ahead to running rules()
(which should only happen if the user inputs 'no' or 'n'), and rules()
runs continuously until stopped, completely skipping input_check()
.
My questions are:
- Why is
input_check()
being completely ignored? - Can you run code taken as a parameter as-is (the
function
parameter) or is there an extra step I need to make it run? - Is there a better/more efficient way to do this? (e.g. a package that parses input that avoids having to make your own function)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看一下这个语句:
当您这样做时,Python 将立即调用
rules
函数,因此它可以将其结果传递给input_check
。您的rules
函数打印出一堆内容,然后有完全相同的行,它将一次又一次地调用rules()
。 ..它永远没有机会调用input_check
。它仍在处理参数。如果您想传递函数对象但不调用它,请不要使用括号:
请注意,
input_check
函数将继续调用传入的函数。您不需要在规则
内再次调用它。后续
这不会按照您的想法进行:
这被解析为:
并且由于“否”为真,因此始终会采用 if 。您应该使用其中之一:
接下来,您有这个:
在这里,您确实想要调用该函数,因此您需要添加括号:
Look at this statement:
When you do that, Python is going to CALL the
rules
function immediately, so it can pass it's result toinput_check
. Yourrules
function prints out a bunch of stuff, then has the exact same line, which is going to callrules()
again, and again, and again, and again... It never gets a chance to callinput_check
. It's still processing the parameters.If you want to PASS the function object but not call it, don't use the parens:
Note that the
input_check
function will keep calling the passed in function. You DON'T need to call it again insiderules
.Followup
This does not do what you think:
That's parsed as:
and since "no" is true, that if will always be taken. You should use one of these:
Next, you have this:
Here, you DO want to call the function, so you need to add the parens:
规则后面不需要任何括号(),而不是传递函数作为运行它的参数。像这样写:-
另外在这里:-
你需要在函数后面添加 (),写:-
让我知道它是否解决了问题
You don't need any parantheses() after rules, instead of passing function as an argument you're running it. Write it like this:-
Also here:-
You need to add () after function, write:-
Let me know if it does solve problem
rules()
作为参数来传递函数。您需要更改为:inputCheck("你知道规则吗?\n",rules,"好的。开始游戏...")
。规则():将调用函数规则()
规则:函数可以作为参数传递给另一个函数。
您可以来这里获取更多信息:
在Python中调用函数时带括号和不带括号有什么区别?。
注意:我看到您的示例代码有很多错误(当像对象或函数一样使用
defrules()
时)。你应该学习如何调试,它将帮助你有效地修复错误rules()
like a parameter to pass a function. You need to change to:inputCheck("Do you know the rules?\n", rules, "Ok. Starting game...")
.rules(): will calling function rules()
rules: functions can be passed as a parameter to another function.
You can come here to get more information:
What is the difference between calling function with parentheses and without in python?.
Note: I saw your sample code has many errors (when using
def rules()
like an object or function). You should to learn how to debug, it will help you fix errors effective与其他答案一起,我发现了另一个语义错误:您的第一个 if 语句将始终评估为 true,因为它将评估“no”的布尔值,如下所示,
因为非空字符串评估为 true,因此该语句将始终执行。除了您所拥有的之外,您还可以添加
给您
制作它,以便仅当 yes_no.lower 为“n”或“no”时该语句才计算为 true
以获取进一步说明,请参阅
为什么我的 python if 语句不起作用?
along with the other answers, I found another semantic error: your first if statement will always evaluate to true, as it will be evaluating the boolean value of 'no' as follows
since a non empty string evaluates to true, this statement will always execute. instead of what you have, you can add
giving you
making it so that statement evaluates as true only if yes_no.lower is 'n' or 'no'
for further clarification, see
Why is my python if statement not working?
您会得到很多关于代码当前行为的解释,但没有
关于如何做我认为你想做的事情的很多实用建议。你不
需要来回传递规则函数。你需要最重要的
用于获取用户输入的工具:while-true 循环。
You're getting a lot of explanation for the code's current behavior but not
much practical advice on how to do what I think you're trying to do. You don't
need to pass the rules function back and forth. You need the most important
tool for getting user input: a while-true loop.