从主功能获取价值

2024-04-20 00:32:55 发布

您现在位置:Python中文网/ 问答频道 /正文

晚上好。我的功能有问题。我有4到5种不同的菜单 我想从main调用Usermeny(),并生成if,elif语句 如果选择==1:做点什么。你知道吗

但是当我选择1时,我得到了一个错误“NameError:global name'Choice'is not defined”

如果我在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

Tags: toin功能youifmaindefhave