从主要功能达到价值

时间:2013-12-26 17:55:00

标签: python

晚上好。我的功能有问题。我有一个4或5个不同的菜单 选择,我想从main调用我的Usermeny()并制作一个if,elif语句 如果选择== 1:做点什么。

但是当我选择1时,我收到此错误“NameError:全局名称'选择'未定义”

如果我在我的Usermeny功能中添加全局选择,我可以从main调用它,但我认为我的返回会有效吗?

在我的主要功能中它看起来像这样

def main():

    TRIES=25
    Usermeny()

    if Choice==1:
        DOSOMETHNIGHERE

用户菜单看起来像这样

def Usermeny():

    while True:
        try:
            global Choice
            Choice=(int(input("What would you like to do: ")))

        except ValueError:
            print("You have to choose a number in the meny")
            continue

        if not Choice in range(1,5):
            print("You have to choose a number in the meny")
            continue

        return Choice

1 个答案:

答案 0 :(得分:4)

删除全局声明,不需要它。

在主函数中将Usermeny函数的结果分配给Choice

def main():
    TRIES=25
    Choice = Usermeny()

    if Choice == 1:
        DOSOMETHNIGHERE