Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
。不要使用Python_Python_Dictionary - Fatal编程技术网

。不要使用Python

。不要使用Python,python,dictionary,Python,Dictionary,运行此代码时,无论我输入什么字典。get函数始终返回错误。当我输入“清除”时,脚本应转到该函数。有人知道为什么这不能正常工作吗?谢谢。您总会看到错误,因为函数的所有参数都必须在调用函数之前求值。因此,在将其作为默认值传递给get()之前,将调用error(choice)以获取其结果 相反,省略默认值,并显式选中它: import os name = input("Please enter your username ") or "name" server = input("Please en

运行此代码时,无论我输入什么字典。get函数始终返回错误。当我输入“清除”时,脚本应转到该函数。有人知道为什么这不能正常工作吗?谢谢。

您总会看到错误,因为函数的所有参数都必须在调用函数之前求值。因此,在将其作为默认值传递给
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

这里有很多递归。它将如何结束?这里有很多递归。它将如何结束?