Going back to the start function stops the script

Viewed 79

I am making a program where you can login, logout and access some settings, but I seem to be encountering an issue after logging out. I'm doing the logout by just calling the function containing the login code, but when it reaches the logout state it just ends the script instead of going back. The Code:

    def main():
        UserName = input ("Enter Username: ")
    
        if UserName == 'a':
            print ("Login successful")
            logged()
    
        else:
            print ("Username is incorrect")
            main()
    
    def logged():
        print ("starting system...")
    
    main()
    
    print("welcome to joop databases")
    
    def options(): 
        print (" ")
        print ("[1]", end =" ")
        print ("Settings")
      
        print(" ")
        Selection = input ("")
      
        if Selection == '1':
            print ("    Settings")
            print (" ")
            print ("[x]", end =" ")
            print ("close")
            print (" ")
            print ("[1]", end =" ")
            print ("Log out")
            print (" ")
            Settings = input('')
            if Settings == 'x':
                options() 
          
            elif Settings == '1':
                main()
          
    options()

Anyway I'm an absolute beginner, I started yesterday, so if you could give simple answers it would be much appreciated haha.

2 Answers

edit your code in here

import sys
def main():
    #your code
    if UserName == 'a':
        logged()
    else:
        main()
    
def logged():
    #your code
    option()

def option():
    #your code
    if Settings == 'x':
        #here i'm considering `x` to quit the program
        sys.exit() 
    elif Settings == '1':
        main()
    else:
        print("invalid input)
        option()

main()

The issue with your code :- you are calling main(), if login successful, you are calling logged().
Then you are calling options() with setting=='1', it'll call main(). and again if login successful, you are calling logged(). But after this, focus will return back to the option() and so your code ends.

First of all, I don't think recursion (calling the function inside itself) is a good way of achieving what you want. recursion is confusing and could lead to weaknesses in your code (for example a user could abuse the recursion limit and crash the program).

A loop is designed for these kind of tasks, try something like:

while True:
    user_name = input('Enter Username: ')
    while user_name != 'a':
        print('Username is incorrect!')
        user_name = input('Enter Username: ')
    # The correct username has been entered!
    print('\nLogin successful!')
    print('Starting system...\n')
    print('Welcome to joop databases\n')
    while True:
        print('~~~~~~~~~~~~~~~~~~~~~~~~~')
        print('[1] Settings')
        print('')
        
        selection = input()
        
        if selection == '1':
            print ('    Settings')
            print ('[x] Close')
            print ('[1] Log out')
            print (' ')
            settings = input()
            if settings == 'x':
                continue
            elif settings == '1':
                break
            else:
                print('Anavailable Option')
        else:
            print('Anavailable Option')

Also a couple of notes:

  • if you want to print a line break you can put '\n' in the string and it will put a line break there
  • if the string parameter of input() is empty, you can just write input() instead of input('')
Related