Python 使用提示符确定要使用的函数
主函数调用了前面定义的两个函数:“reg()”和“usa()”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
我希望用户能够决定运行哪个函数。
但是,即使选择了“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