0

EDIT: Seems like people doesen't understand the point... I have three diferent functions that can call one common function. How to back from that common function to right previous function depending of user role?

I have login function like this:

def login():
    users = open('users.txt', 'r').readlines()
    username = input("\nKorisnicko ime: ")
    password = input("Lozinka: ")
    for i in users:
        users=i.strip("\n").split("|")
        if (username == users[0] and password == users[1]):
            print("\nDobrodosli: " + users[2].title(), users[3].title()+"\n" )
            if users[4] == 'kupac':
                buyer_menu()
            elif users[4] == 'prodavac':
                seller_menu()
            elif users[4] == 'menadzer':
                manager_menu()

And buyer, seller and manager functions like this:

def buyer_menu():
    print("=== BUYER ===")
    print("1. Search movie")
    print("2. Search moive")
    print("7. Logout")
    print("8. Exit")
    options = input("\nChoose option >>> ")
    if options == '1':
        search_movie()
    elif options == '2':
        search.search_movie()
    ...
    elif options == '8':
        print ("Predstava je otkazana. Nestalo struje.")
        exit()
    else:
        print("Nepostojeca opcija!\n")
        buyer_menu()

Seller.

def seller_menu():
        print("=== SELLER ===")
        print("1. Search movie")
        print("2. Search ticket")
        print("7. Back to main menu")
        options = input("\nChoose option >>> ")
        if options == '1':
            search_movie()
        elif options == '2':
            search.search_movie()
        elif options == '7':
            main()
        elif options == '8':
            print ("Predstava je otkazana. Nestalo struje.")
            exit()
        else:
            print("Nepostojeca opcija!\n")
            buyer_menu()

Manager:

def manager_menu():
        print("=== MANAGE ===")
        print("1. Search movie")
        print("2. Search users")
        print("8. Exit")
        options = input("\nChoose option >>> ")
        if options == '1':
            search_movie()
        elif options == '2':
            search.search_users()
        elif options == '8':
            print ("Predstava je otkazana. Nestalo struje.")
            exit()
        else:
            print("Nepostojeca opcija!\n")
            buyer_menu()

Ass you can see all trhee type of users have "Search movie" options, and function for that looks like this:

def search_movie():
    print("\n1. Search movie title")
    print("2. Search movie genre")

    print("8. Back to main menu")

    options = input("\nUnesite kriterijum za pretragu >>> ") 
    if options == '1':
        search_movie_title()
    elif options == '2':
        search_movie_genre()

Now, if you see, problem is function for back to main menu.... For example: If I'm logged in as a buyer and I choose search movie option, but then I want to back to main menu, how to back on buyer_menu(): And so on fo other type of users... seller on seller_menu(): and manager on manager_menu():

Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77
Neo Cortex
  • 47
  • 1
  • 11
  • 1
    Why not just check the type of the user, and then redirect to the proper menu? For example, if you detect that the user who wants to go back to the main menu is a buyer, then send him to buyer menu. – Muntaser Ahmed Jan 17 '18 at 19:40
  • Have a dictionary (or list) containing references to functions. When you decide on the user type, populate it with the functions that are relevant for that user. When it comes to execute a function, just use the reference, that way you don't have to keep checking. – cdarke Jan 17 '18 at 19:44
  • @MuntaserAhmed Ummm yes? That's a question... how to do that? – Neo Cortex Jan 17 '18 at 22:19

3 Answers3

1

Use a while loop

Best practice, when trying to return to a CLI menu, is to fire menus of this kind from within a while loop like so.

if users[4] == 'kupac':
    # Or some other suitable condition.
    while not exit:
        buyer_menu()

With this your user will fall back to the login function once they have reached the end of the call stack. Then the next while loop iteration will bring them back to the correct menu.

The "7. Back to main menu" option should simply allow the current function to finish, and returns up the call stack, back to the while loop, which would then call the correct method for the user's role again. We do it this way so that we do not continuously grow the call stack until the stack overflows.

Examples and references

Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77
1

Can't you just pass the previous menu to search_movie() instead?

def search_movie(user_menu):
    #...#
    elif options == '8'
        user_menu() # call the previous user menu again

When you call the search_movie(), just include the previous function name:

def buyer_menu():
    #...#
    if options == '1':
        search_movie(buyer_menu) #include the context
    elif options == '2':
        search.search_movie(buyer_menu) #include the context
    #...#

At a glance your code looks like it could use with some optimizing, but that's not part of the question and too big of a bear for me to poke right now.

r.ook
  • 13,466
  • 2
  • 22
  • 39
1

First solution: You can pass user object thru functions like this:

def login():
    users = open('users.txt', 'r').readlines()
    username = input("\nKorisnicko ime: ")
    password = input("Lozinka: ")
    for i in users:
        users=i.strip("\n").split("|")
        if (username == users[0] and password == users[1]):
            print("\nDobrodosli: " + users[2].title(), users[3].title()+"\n" )
            if users[4] == 'kupac':
                buyer_menu(users[4])
            elif users[4] == 'prodavac':
                seller_menu(users[4])
            elif users[4] == 'menadzer':
                manager_menu(users[4])

With this solution, you are passing users role to function, this is how new function will know what is this users role.

Solution #2: Since you are using files as a database, you can also save what user is logged in (only his credentials) in some currentUser.txt file, and every time call some function that reads that file to check who is logged in.

Solution #3: Create one function that only checks users role and gives back this 'basic' menu. Something like this:

def showStartMenu(currentUsersUsername):
    users = open('users.txt', 'r').readlines()
    for i in users:
        users=i.strip("\n").split("|")
        if (currentUsersUsername== users[0]):
            if users[4] == 'kupac':
                buyer_menu()
            elif users[4] == 'prodavac':
                seller_menu()
            elif users[4] == 'menadzer':
                manager_menu()

Just remember, you need to pass current users username. So, just use combination with solution 2 and 3

EDIT: Adding example to FIRST SOLUTION

After you send users[4] to functions you use it like this:

def seller_menu(usersRole):
        print("=== SELLER ===")
        print("1. Search movie")
        print("2. Search ticket")
        print("7. Back to main menu")
        options = input("\nChoose option >>> ")
        if options == '1':
            search_movie(usersRole)
        elif options == '2':
            search.search_movie()
        elif options == '7':
            main()
        elif options == '8':
            print ("Predstava je otkazana. Nestalo struje.")
            exit()
        else:
            print("Nepostojeca opcija!\n")
            buyer_menu()

And search menu now:

def search_movie(usersRole):
    print("\n1. Search movie title")
    print("2. Search movie genre")

    print("8. Back to main menu")

    options = input("\nUnesite kriterijum za pretragu >>> ") 
    if options == '1':
        search_movie_title()
    elif options == '2':
        search_movie_genre()
    elif options == '8':
        if usersRole == 'kupac':
            buyer_menu()
        elif usersRole == 'prodavac':
            seller_menu()
        elif usersRole == 'menadzer':
            manager_menu()
Dale12
  • 30
  • 6