无法通过输入菜单访问代码内的功能

发布于 2025-01-19 13:17:45 字数 1951 浏览 1 评论 0原文

我无法访问输入菜单中其余代码中的功能 用户应该输入一个字母,该字母将带他到程序的函数并继续按函数状态进行编辑,但我收到一条错误消息,指出“NameError:名称'reg_user'未定义”。

def display_menu_options():    

    global menu_options

    menu_options = input('''Please select one of the following options:

a - Adding a task
va - View all tasks
vm - View my tasks
e - Exit
:''').lower()

    if menu_options == "a":
        add_task()
    elif menu_options == "va":
        view_all()
    elif menu_options == "vm":
        view_mine()
    elif menu_options == "e":
        print("Goodbye!")
    else:
        print("You have made a wrong choice, Please Try again")

    return menu_options

def display_admin_menu_options():

    global menu_options

    menu_options = input('''Please enter one of the following options:
    
r - register user
a - add task
va- view all tasks
vm - view my tasks
gr - generate reports
ds - display statistics
e - exit
:''').lower()

    if menu_options == "r":
        reg_user()
    elif menu_options == "a":
        add_task()
    elif menu_options == "va":
        view_all()
    elif menu_options == "vm":
        view_mine()
    elif menu_options == "gr":
        generate_reports()
    elif menu_options == "ds":
        all_statistics()
    elif menu_options == "e":
        print("Goodbye!")
    else:
        print("You have made a wrong choice, Please Try again")

    return menu_options

def login():

    username = input("Enter your username: ")
    password = input("Enter your password: ")

    for line in open('user.txt', 'r').readlines():
        field = line.strip().split(", ")
        if username == field[0] and password == field[1]:
            print('Welcome' + username)
            return True, field[0] == "admin"

    return False, False

login_success, is_admin = login()

if login_success and is_admin:
    display_admin_menu_options()
elif login_success:
    display_menu_options()
else:
    print("Username or password incorrect!")

I cannot access the functions in the rest of the code in the input menu
The user is supposed to input a letter to which that letter will take him to the function of the program and continue editing as the function states, but I get an error message stating "NameError: name 'reg_user' is not defined".

def display_menu_options():    

    global menu_options

    menu_options = input('''Please select one of the following options:

a - Adding a task
va - View all tasks
vm - View my tasks
e - Exit
:''').lower()

    if menu_options == "a":
        add_task()
    elif menu_options == "va":
        view_all()
    elif menu_options == "vm":
        view_mine()
    elif menu_options == "e":
        print("Goodbye!")
    else:
        print("You have made a wrong choice, Please Try again")

    return menu_options

def display_admin_menu_options():

    global menu_options

    menu_options = input('''Please enter one of the following options:
    
r - register user
a - add task
va- view all tasks
vm - view my tasks
gr - generate reports
ds - display statistics
e - exit
:''').lower()

    if menu_options == "r":
        reg_user()
    elif menu_options == "a":
        add_task()
    elif menu_options == "va":
        view_all()
    elif menu_options == "vm":
        view_mine()
    elif menu_options == "gr":
        generate_reports()
    elif menu_options == "ds":
        all_statistics()
    elif menu_options == "e":
        print("Goodbye!")
    else:
        print("You have made a wrong choice, Please Try again")

    return menu_options

def login():

    username = input("Enter your username: ")
    password = input("Enter your password: ")

    for line in open('user.txt', 'r').readlines():
        field = line.strip().split(", ")
        if username == field[0] and password == field[1]:
            print('Welcome' + username)
            return True, field[0] == "admin"

    return False, False

login_success, is_admin = login()

if login_success and is_admin:
    display_admin_menu_options()
elif login_success:
    display_menu_options()
else:
    print("Username or password incorrect!")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韵柒 2025-01-26 13:17:45

我假设您正在执行并将其编写为脚本。因此,您正在调用该函数,并在定义您要访问的函数的下方。这些不是“定义的”,因为代码是解释的而不是编译的。将所有函数定义放在 login() 调用之上

I assume you are executing and writing it as a script. Therefore you're calling the function and beneath your defining the functions you're trying to access. Those are not "defined" because the code is interpreted and not compiled. Put all function definitions above the login() call

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文