Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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_Function_Prompt - Fatal编程技术网

Python 使用提示符确定要使用的函数

Python 使用提示符确定要使用的函数,python,function,prompt,Python,Function,Prompt,主函数调用了前面定义的两个函数:“reg()”和“usa()” 我希望用户能够决定运行哪个函数。 但是,即使选择了“1”,仍然会运行“usa()” def main(): prompt = (input("If you would like to change the regional market numbers, press 1." "If you would like to change the na

主函数调用了前面定义的两个函数:“reg()”和“usa()”
我希望用户能够决定运行哪个函数。
但是,即使选择了“1”,仍然会运行“usa()”

def main():
    prompt = (input("If you would like to change the regional market numbers,
                     press 1."
                    "If you would like to change the national market, press 2."))
    if prompt == 1:
        a = reg()
        a
    else:
        b = usa()
        b


main()

如果您使用的是Python3,那么
input()
将返回一个字符串,与整数相比,该字符串将始终返回false。无需使用if和else语句来决定使用哪个函数:

def main():
    functions = {1: reg, 2: usa}
    prompt_message = 'If you would like to change the regional market numbers, press 1.\nIf you would like to change the national market, press 2.'
    prompt = str(input(message))
    function = functions[prompt]

您使用哪一版本的python?\
prompt
在Py3k中总是
str
,将其与
int
进行比较将始终显示
False