使用提示确定要使用的功能

时间:2014-05-16 03:53:13

标签: 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 national market, press 2."))
    if prompt == 1:
        a = reg()
        a
    else:
        b = usa()
        b


main()

1 个答案:

答案 0 :(得分:0)

如果您使用的是Python 3,则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]