Jump to content

[Python] Why can't i call the function?

Go to solution Solved by Neil,

Best practice for this sort of stuff would look something like this:

 

https://gist.github.com/NeilHanlon/aecca949a4fa764127bd

 

Essentially what I did was abstract your "main selection" code so it's only in one place. You had a lot of printing the same thing over and over, which isn't good code.

 

Next, I added the following to the bottom:

 if __name__ == '__main__': #__name__ is a superglobal that is set to __main__ if the file is called by execution of the actual file (not by importing, etc), so this will only run if you do "python file.py" or run it from an IDE.    """Create Dictionary "options" with a relational mapping between the user input (integer) and a function name. Therefore we can call options[1]() to have it run login_Driver(), etc."""    options = {        1: login_Driver,        2: login_Customer,        3: create_Account,    }     opt = main_Selection() #main_Selection method now returns the user's input.     while opt is not 4: # while the user hasn't entered a 4 (exit), continue into this block.        options[opt]() #call the function the user is looking for, based on the above dictionary        opt = main_Selection() #re-run main_Selection to return the new input from the user.

EDIT: FFs... it cut off the entire end of my response...

 

I'm not re-typing it. If you need any help or have questions, please let me know. I'm happy to share my knowledge

hey guys i have to do a small group project of a washing company's software and stuff. i am not good at python and as you can see in the code that the login_Driver() function has an if statement in the end which calls another function if true but its not calling the function.

def login_Driver():    print("Welcome to Sky Laundry")    print("Login Driver Menu")    print("Enter Username")    driver_username = str(input())    print("Enter Password")    driver_password = str(input())    '''text_file = open("driver_username.txt", "w")    text_file.write(driver_username)    text_file.close()'''    text_file = open("driver_username.txt", "r")    driver_username_check = text_file.readline()    text_file.close()    text_file = open("driver_password.txt", "r")    driver_password_check = text_file.readline()    text_file.close()    if driver_username_check == driver_username and driver_password_check == driver_password:        print("Login successful")        return driver_username        driver_Account()    else:        print("Login failed")    def login_Customer():    print("Welcome to Sky Laundry")    print("Login Customer")    print("Enter Username")    customer_username = str(input())    print("Enter Password")    customer_password = str(input())    '''text_file = open("customer_username.txt", "w")    text_file.write(customer_username)    text_file.close()'''    text_file = open("customer_username.txt", "r")    customer_username_check = text_file.readline()    text_file.close()    text_file = open("customer_password.txt", "r")    customer_password_check = text_file.readline()    text_file.close()    if customer_username_check == customer_username and customer_password_check == customer_password:        print("Login successful")        customer_Account()        return customer_username    else:        print("Login failed")def create_Account():    print("Welcome to Sky Laundry")    print("Enter whom you want to register as?")    print("1.) Driver")    print("2.) Customer")    choice = int(input())    if choice == 1:        print("Driver Registration Menu")        print("Enter your Username")        new_driver_username = str(input())        print("Enter you Password")        new_driver_password = str(input())                text_file = open("driver_username.txt", "w")        text_file.write(new_driver_username)        text_file.close()        text_file = open("driver_password.txt", "w")        text_file.write(new_driver_password)        text_file.close()        print("Registration Successful, taking you back to the Main Menu")        main_Selection()    elif choice == 2:        print("Customer Registration Menu")        print("Enter your Username")        new_customer_username = str(input())        print("Enter you Password")        new_customer_password = str(input())                text_file = open("customer_username.txt", "w")        text_file.write(new_customer_username)        text_file.close()        text_file = open("customer_password.txt", "w")        text_file.write(new_customer_password)        text_file.close()        print("Registration Successful, taking you back to the Main Menu")        main_Selection()    else:        print("INVALID OPTION")def driver_Account():    print("Drivers Account")def customer_Account():    print(customer_account_name = login_Customer())    print("Account")print("Welcome to Sky Laundry")print("1.) Login Driver")print("2.) Login Customer")print("3.) Create Account")print("4.) User Exit")def main_Selection():    print("Welcome to Sky Laundry")    print("1.) Login Driver")    print("2.) Login Customer")    print("3.) Create Account")    print("4.) User Exit")        if opt is 1:        login_Driver()    elif opt is 2:        login_Customer()    elif opt is 3:        create_Account()    elif opt is 4:        login_Driver()    else:        print("INVALID OPTION PLEASE TRY AGAIN")        main_Selection()opt = int(input())main_Selection()                

as you can see that it reads from external txt files with usernames and passwords those files are attached if someone wants to test the program.

customer_password.txt

customer_username.txt

driver_password.txt

driver_username.txt

Link to comment
https://linustechtips.com/topic/362031-python-why-cant-i-call-the-function/
Share on other sites

Link to post
Share on other sites

return driver_usernamedriver_Account()

You return before calling function. Don't know python, but usually in other languages nothing is done after return. Try to change it to:

driver_Account()return driver_username

I am sorry for my english.

Link to post
Share on other sites

return driver_usernamedriver_Account()

You return before calling function. Don't know python, but usually in other languages nothing is done after return. Try to change it to:

driver_Account()return driver_username

thanks man it fixed that but do you know why its repeating the function print statements again?

Link to post
Share on other sites

thanks man it fixed that but do you know why its repeating the function print statements again?

if you look at how your menu works, you will see that it will ask for the user choice only once

if the user chooses 1, the menu will keep entering option 1 infinitely

Link to post
Share on other sites

Best practice for this sort of stuff would look something like this:

 

https://gist.github.com/NeilHanlon/aecca949a4fa764127bd

 

Essentially what I did was abstract your "main selection" code so it's only in one place. You had a lot of printing the same thing over and over, which isn't good code.

 

Next, I added the following to the bottom:

 if __name__ == '__main__': #__name__ is a superglobal that is set to __main__ if the file is called by execution of the actual file (not by importing, etc), so this will only run if you do "python file.py" or run it from an IDE.    """Create Dictionary "options" with a relational mapping between the user input (integer) and a function name. Therefore we can call options[1]() to have it run login_Driver(), etc."""    options = {        1: login_Driver,        2: login_Customer,        3: create_Account,    }     opt = main_Selection() #main_Selection method now returns the user's input.     while opt is not 4: # while the user hasn't entered a 4 (exit), continue into this block.        options[opt]() #call the function the user is looking for, based on the above dictionary        opt = main_Selection() #re-run main_Selection to return the new input from the user.

EDIT: FFs... it cut off the entire end of my response...

 

I'm not re-typing it. If you need any help or have questions, please let me know. I'm happy to share my knowledge

--Neil Hanlon

Operations Engineer

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×