因此,我被指派使用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参数,请尝试以下操作:
当您想使用该功能时,请执行以下操作:
您还应将用户名输入get_passenger。username是一个局部变量,因此get_passenger无权访问它,因此会出现错误
而且,返回用户名在这里没有任何作用,所以我删除了它
相关问题 更多 >
编程相关推荐