Python Forum
Passing a function to another function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Passing a function to another function (/thread-23886.html)



Passing a function to another function - yulfmd1802 - Jan-22-2020

How do you pass a certain value from a function to another function? Previously I know how to pass a value from a function to the main() function. Attempting to pass the value with the code attached below the following error message produces the errors:

Traceback (most recent call last):
File "/Users/fadhil/Downloads/get_passenger.py", line 25, in <module>
main()
File "/Users/fadhil/Downloads/get_passenger.py", line 6, in main
get_ticket()
File "/Users/fadhil/Downloads/get_passenger.py", line 12, in get_ticket
ticket = get_passenger(username)
NameError: name 'username' is not defined

Any suggestions, please let me know. Thanks for the advance!

def main():
    print("Welcome to the Tropical Airlines Ticket Ordering System")
    print("Log in to the system using your username. A password is not required.")
    username = input("Username: ")
    login(username)
    get_ticket()


def get_ticket():
    ticket = input("This is the ticket for: ")
    if ticket == "you":
        ticket = get_passenger(username)
    else:
        ticket = "For someone else"
    print(ticket)

def login(username):
    print("Welcome, " + username)
    return username

def get_passenger(username):
    passenger = print("Dear " + login(username))
    return passenger

main()



RE: Passing a function to another function - jefsummers - Jan-22-2020

getticket() does not have access to username. You could pass username from main()


RE: Passing a function to another function - michael1789 - Jan-22-2020

Try telling getticket() to accept username then give it username when you call it. You already do that with login(username).

def main():
    print("Welcome to the Tropical Airlines Ticket Ordering System")
    print("Log in to the system using your username. A password is not required.")
    username = input("Username: ")
    login(username)
    get_ticket(username)
 
 
def get_ticket(username):
    ticket = input("This is the ticket for: ")
    if ticket == "you":
        ticket = get_passenger(username)
    else:
        ticket = "For someone else"
    print(ticket)



RE: Passing a function to another function - ndc85430 - Jan-23-2020

Note that the title of the thread is misleading: you aren't passing a function to another function, you're passing the value returned by a function to another function. The two are different things!