。不要使用Python
运行此代码时,无论我输入什么字典。get函数始终返回错误。当我输入“清除”时,脚本应转到该函数。有人知道为什么这不能正常工作吗?谢谢。您总会看到错误,因为函数的所有参数都必须在调用函数之前求值。因此,在将其作为默认值传递给。不要使用Python,python,dictionary,Python,Dictionary,运行此代码时,无论我输入什么字典。get函数始终返回错误。当我输入“清除”时,脚本应转到该函数。有人知道为什么这不能正常工作吗?谢谢。您总会看到错误,因为函数的所有参数都必须在调用函数之前求值。因此,在将其作为默认值传递给get()之前,将调用error(choice)以获取其结果 相反,省略默认值,并显式选中它: import os name = input("Please enter your username ") or "name" server = input("Please en
get()
之前,将调用error(choice)
以获取其结果
相反,省略默认值,并显式选中它:
import os
name = input("Please enter your username ") or "name"
server = input("Please enter a name you wish to call this server ") or "server"
prompt = name + "@" + server
def error(choice):
print(choice + ": command not found")
commands()
def clear():
os.system("cls")
commands()
def commands():
while 1 > 0:
choice = input(prompt)
{'clear': clear}.get(choice, error(choice))()
commands()
您总是会看到错误,因为在调用函数之前必须对函数的所有参数求值。因此,在将其作为默认值传递给
get()
之前,将调用error(choice)
以获取其结果
相反,省略默认值,并显式选中它:
import os
name = input("Please enter your username ") or "name"
server = input("Please enter a name you wish to call this server ") or "server"
prompt = name + "@" + server
def error(choice):
print(choice + ": command not found")
commands()
def clear():
os.system("cls")
commands()
def commands():
while 1 > 0:
choice = input(prompt)
{'clear': clear}.get(choice, error(choice))()
commands()
您不想实际调用
error(choice)
您只能将其留待以后调用:
result = {'clear': clear}.get(choice)
if result:
result()
else:
error(choice)
所以你想要:
>>> def error(choice):
... print(choice + ': command not found')
>>> from functools import partial
>>> func = partial(error, choice='asdf')
>>> func()
asdf: command not found
您不想实际调用
error(choice)
您只能将其留待以后调用:
result = {'clear': clear}.get(choice)
if result:
result()
else:
error(choice)
所以你想要:
>>> def error(choice):
... print(choice + ': command not found')
>>> from functools import partial
>>> func = partial(error, choice='asdf')
>>> func()
asdf: command not found
这里有很多递归。它将如何结束?这里有很多递归。它将如何结束?