Kalin-the-Builder
Kalin-the-Builder

Reputation: 49

Calling displayMenu() function after user logged in

The user should log in with his/ her username & password. If the user is admin then he should get his own menu with extra options. The issue I'm having is that upon successful login is that the displayMenu() or displayMenu_Admin() isn't called. There is a text file called 'user.txt' that the username and passwords gets saved in.

#The user should be prompted to enter a username and
#password. A list of valid usernames and passwords are stored in "user.txt".
#Display an appropriate error message if the
#user enters a username that is not listed in user.txt or enters a valid
#username but not a valid password. The user should repeatedly be
#asked to enter a valid username and password until they provide
#appropriate credentials.

def login():
        username = input("Please enter your username?:\n")
        password = input("Please enter your password?:\n")

        for line in open("user.txt","r").readlines():
                field = line.strip().split(",")
                if username == field[0] and password == field[1]:
                        print("Hello " + username + ", welcome back!\n")
                        return True
        if field[0] == "admin":
                displayMenu_Admin()
        else:
                displayMenu()
        print("Username or Password Incorrect\n")
        return False
login()

def displayMenu_Admin():
        global menu_input

        menu_input = input("Please enter one of the following options:\n r - register user\n a - add task\n va- view all tasks\n vm - view my tasks\n s - statistics\n e - exit\n")

        if menu_input == "r":
                register()
        elif menu_input == "a":
                add_task()
        elif menu_input == "va":
                view_all()
        elif menu_input == "vm":
                view_more()
        elif menu_input == "s":
                statistic() 
        elif menu_input == "e":
                exit()

        return menu_input

#A menu should be displayed once the user has successfully logged in.

def displayMenu():
        global menu_input

        menu_input = input("Please enter one of the following options:\n a - add task\n va- view all tasks\n vm - view my tasks\n e - exit\n")

        if menu_input == "a":
                add_task()
        elif menu_input == "va":
                view_all()
        elif menu_input == "vm":
                view_more()
        elif menu_input == "e":
                exit()

        return menu_input

The idea was that the user should login in and if the user is the 'admin' user a other menu should be displayed. Any advice or help would be greatly appreciated!

Upvotes: 0

Views: 582

Answers (2)

Kalin-the-Builder
Kalin-the-Builder

Reputation: 49

The fix as provided above was moving the 'return True' to after displayMenu()

def login():
        username = input("Please enter your username?:\n")
        password = input("Please enter your password?:\n")

        for line in open("user.txt","r").readlines():
                field = line.strip().split(",")
                if username == field[0] and password == field[1]:
                        print("Hello " + username + ", welcome back!\n")

                        if field[0] == "admin":
                            displayMenu_Admin()
                        else:
                            displayMenu()
                        return True
        print("Username or Password Incorrect\n")
        return False

Upvotes: 0

nick
nick

Reputation: 459

Your problem is that you are returning if the login is successful, thus the block that calls the display functions is never executed.

def login():
        username = input("Please enter your username?:\n")
        password = input("Please enter your password?:\n")

        for line in open("user.txt","r").readlines():
                field = line.strip().split(",")
                if username == field[0] and password == field[1]:
                        print("Hello " + username + ", welcome back!\n")
                        return True # <-- this is causing your problems
        if field[0] == "admin":
                displayMenu_Admin()
        else:
                displayMenu()
        print("Username or Password Incorrect\n")
        return False

To fix your problems, just return after displaying the menu


def login():
        username = input("Please enter your username?:\n")
        password = input("Please enter your password?:\n")

        for line in open("user.txt","r").readlines():
                field = line.strip().split(",")
                if username == field[0] and password == field[1]:
                        print("Hello " + username + ", welcome back!\n")

                        if field[0] == "admin":
                            displayMenu_Admin()
                        else:
                            displayMenu()
                        return True
        print("Username or Password Incorrect\n")
        return False

However, it may be a better design to instead return a tuple and let somebody else handle the menu stuff, such as:

def login():
        username = input("Please enter your username?:\n")
        password = input("Please enter your password?:\n")

        for line in open("user.txt","r").readlines():
                field = line.strip().split(",")
                if username == field[0] and password == field[1]:
                        print("Hello " + username + ", welcome back!\n")
                        return True, field[0]== "admin"

        return False, False

login_success, is_admin = login()

if login_success and is_admin:
   displayMenu_Admin()
elif login_success:
   displayMenu()
else:
   print("Username or Password Incorrect\n")

Upvotes: 1

Related Questions