Python 如何在两个函数内传递值而不显示错误?

Python 如何在两个函数内传递值而不显示错误?,python,Python,因此,我被指派使用Python创建一个排序应用程序。我可以通过login函数登录程序并打印“Welcome{user}”,但是当调用passenger是用于登录get_passenger函数的用户名时,程序会崩溃并产生以下错误: 回溯(最近一次呼叫最后一次): main()第36行的文件“/Users/fadhil/Downloads/getpassenger.py” 文件“/Users/fadhil/Downloads/getpassenger.py”,第9行,主get_passenger()

因此,我被指派使用Python创建一个排序应用程序。我可以通过login函数登录程序并打印“Welcome{user}”,但是当调用passenger是用于登录get_passenger函数的用户名时,程序会崩溃并产生以下错误:

回溯(最近一次呼叫最后一次):

main()第36行的文件“/Users/fadhil/Downloads/getpassenger.py” 文件“/Users/fadhil/Downloads/getpassenger.py”,第9行,主get_passenger()
文件“/Users/fadhil/Downloads/getpassenger.py”,第27行,在get_passenger passenger=login(用户名)中

名称错误:未定义名称“username”

以及如何从另一个函数调用这种函数?我还有一个函数叫order(),我想在order()函数中调用get_passenger()

所附代码:

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)
    ticket = input("This is the ticket for: ")
    if ticket == "you":
        ticket = get_passenger(username)
    else:
        ticket = "For someone else"

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

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

main()

这是因为在定义get_passenger()函数时没有传递passenger参数,请尝试以下操作:

def get_passenger(username):
    passenger = login(username)
    print("Dear " + passenger)
    return passenger
当您想使用该功能时,请执行以下操作:

get_passenger(username)

您还应将用户名输入get_passenger。username是一个局部变量,因此get_passenger无权访问它,因此会出现错误

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)
    #After login, show the welcome message, and then the menu options
    get_passenger(username)
    #If any wrong input is made within the program, force the program to quit

def login(username):
    #Log in to the system using the username
    print("Welcome, " + username)
    #After login, show the welcome message, and then the menu options

def get_passenger(username):
    print("Dear " + username)

此外,返回用户名在这里没有任何作用,因此我将其删除。

请将函数定义更改为:

def get_passenger(username):
把它叫做:

get_passenger(username)

我的意思是,如果它不在主函数中,如何调用它。我在order()函数中调用了get_passenger(),不将代码放在图片中,没有人想键入,复制粘贴为文本