[英]Why can't I return my "cmds" variable in my code?
I want to create a command robot but I cant return cmds(variable).我想创建一个命令机器人,但我无法返回 cmds(变量)。 It always crashes not giving me anything.它总是崩溃没有给我任何东西。 The output is just blank after I enter n in variable con.在变量 con 中输入 n 后,输出只是空白。 I've been working on the problem for the past 2 days.在过去的两天里,我一直在解决这个问题。 It's really annoying and I hope someone can help.这真的很烦人,我希望有人能提供帮助。
import time
print("Hi User! Pick a name for me!")
time.sleep(1)
name = input("Name for Bot: ")
g = print("Ooh", name + "!", "thats a cool name!")
print("What should I call you?")
time.sleep(1)
user_name = input("Your name: ")
g1 = print(user_name + "...", "cool name!")
time.sleep(1)
cmds = input("Say any command you would like :) - ")
def slap(cmds):
slap_p = input("Who do you want to slap: ")
caption_for_slap = input("Caption for slapping: ")
print("Loading...")
time.sleep(3)
print(caption_for_slap, "👊👊👊👊👊👊", ". You deserved it", slap_p)
con = input("Would you like to continue y/n - ")
def con_p(cmds, con):
if con == "y":
return slap()
elif con == "n":
return cmds
con_p()
if cmds == "./slap":
slap(cmds)
while True:
if cmds == "br":
break
def about_rb():
if cmds == "./about_rb":
print("I am a robot named", name + ".", "Thanks to", user_name, "for picking that name for me!")
time.sleep(1)
print("My age is 35 :)")
if cmds == "./about_rb":
about_rb()
def question():
if cmds == "do you like Siri or Cortana":
print("I love all robots! I dont really have a preference for both ♥")
When issuing ./slap
as a command, there are two problems in this part:当./slap
作为命令发出时,这部分有两个问题:
con = input("Would you like to continue y/n - ")
def con_p(cmds, con):
if con == "y":
return slap()
elif con == "n":
return cmds
con_p()
In the final line of this block, the newly created function con_p
is called, but without any of the arguments ( cmds
, con
).在这个块的最后一行,新创建的函数con_p
被调用,但没有任何参数( cmds
, con
)。 Since there is no default value for them either, the script crashes.由于它们也没有默认值,因此脚本会崩溃。
To resolve this, we just need to provide the required arguments;为了解决这个问题,我们只需要提供所需的参数; change the last line to:将最后一行更改为:
con_p(cmds, con)
You might think that this is a bit unnecessary, as you've already used those names in the line def con_p(cmds, con):
, but that's just what the provided arguments will be known as inside the function con_p()
, not where they're pulled from.您可能认为这有点不必要,因为您已经在def con_p(cmds, con):
行中使用了这些名称,但这正是函数con_p()
中提供的参数的名称,而不是 where他们被拉出来了。
The second problem occurs when con_p()
is called with con = "y"
: then con_p()
calls slap()
again without arguments.第二个问题发生在con_p()
使用con = "y"
调用时:然后con_p()
slap()
而没有参数。 We need to replace return slap()
with return slap(cmds)
.我们需要将return slap()
替换为return slap(cmds)
。
Side note : I don't know if there is any further intent with the code block I copy/pasted above, but as is, the con_p() function can be removed.旁注:我不知道我在上面复制/粘贴的代码块是否有任何进一步的意图,但可以删除 con_p() 函数。 The entire block can be replaced with:整个块可以替换为:
con = input("Would you like to continue y/n - ")
if con == "y":
return slap(cmds)
cmds
also isn't used inside slap()
, so we can also just define it as def slap():
instead of def slap(cmds):
. cmds
也没有在slap()
中使用,因此我们也可以将其定义为def slap():
而不是def slap(cmds):
。
As the code is executed from the first line to the last, the following block of code will always keep looping unless cmds
equals br
.由于代码从第一行执行到最后一行,除非cmds
等于br
,否则以下代码块将始终保持循环。
while True:
if cmds == "br":
break
Since we cannot adjust cmds
during the loop, and it is asked only once, we have three scenarios based on the value of cmds
:由于我们无法在循环期间调整cmds
,并且只询问一次,因此基于cmds
的值我们有三种情况:
./slap
so we can slap people, and after we quit slapping ( con == "n"
) we will get in the infinite loop ./slap
这样我们就可以打人了,在我们停止打人之后( con == "n"
)我们将进入无限循环br
so we don't get caught in the loop, no other commands are executed, and the program exits without doing anything br
所以我们不会陷入循环,不会执行其他命令,程序什么也不做就退出cmds
which doesn't get evaluated since we just end up in the loop forever. cmds
的任何其他值都不会被评估,因为我们只是永远处于循环中。I don't know for sure the intended purpose of this loop, but I think you want to put a loop around the entire code that asks for a command input and executes them, as such:我不确定这个循环的预期目的,但我认为你想在整个代码周围放置一个循环,要求输入命令并执行它们,如下所示:
def slap(cmds):
[...]
def about_rb():
[...]
while True:
cmds = input("Say any command you would like :) - ")
if cmds == "./slap":
slap(cmds)
if cmds == "./about_rb":
about_rb()
if cmds == "br":
break
Here, we first define all our functions.在这里,我们首先定义我们所有的函数。 Then in an endless loop we keep asking the user for a new command, and run that command.然后在无限循环中,我们不断向用户询问新命令,并运行该命令。 Only when the command is "br", we break the loop and the computer continues with the code after the loop (if there is any).只有当命令是“br”时,我们才打破循环,计算机继续循环之后的代码(如果有的话)。
# imports
import time
# define functions
def slap(cmds):
slap_p = input("Who do you want to slap: ")
caption_for_slap = input("Caption for slapping: ")
print("Loading...")
time.sleep(3)
print(caption_for_slap, "👊👊👊👊👊👊", ". You deserved it", slap_p)
con = input("Would you like to continue y/n - ")
if con == "y":
return slap(cmds)
def about_rb():
if cmds == "./about_rb":
print("I am a robot named", name + ".", "Thanks to", user_name, "for picking that name for me!")
time.sleep(1)
print("My age is 35 :)")
# name the bot
print("Hi User! Pick a name for me!")
time.sleep(1)
name = input("Name for Bot: ")
g = print("Ooh", name + "!", "thats a cool name!")
# name the user
print("What should I call you?")
time.sleep(1)
user_name = input("Your name: ")
g1 = print(user_name + "...", "cool name!")
time.sleep(1)
# start main loop
while True:
# get command
cmds = input("Say any command you would like :) - ")
if cmds == "br":
break
if cmds == "./slap":
slap(cmds)
if cmds == "./about_rb":
about_rb()
if cmds == "do you like Siri or Cortana":
print("I love all robots! I dont really have a preference for both ♥")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.