Python Forum
Passing a function to another function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing a function to another function
#1
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()
Reply
#2
getticket() does not have access to username. You could pass username from main()
Reply
#3
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)
Reply
#4
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  GCF function w recursion and helper function(how do i fix this Recursion Error) hhydration 3 2,558 Oct-05-2020, 07:47 PM
Last Post: deanhystad
  Adding a sqrt function syntax error and <build-in function sqrt> from tkinter import Kael 0 1,872 Oct-14-2019, 05:51 PM
Last Post: Kael
  Calling a function from another function johneven 2 2,891 Jul-09-2019, 12:42 AM
Last Post: micseydel
  passing a value to function problems Ponamis 2 2,058 Nov-14-2018, 05:05 PM
Last Post: Larz60+
  Need of return in function if statement inside the function already returns Athul 5 3,929 Aug-16-2018, 10:19 AM
Last Post: DuaneJack

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020