Python实现航空公司等级计算时遇NameError: name未定义问题求助
NameError & Other Issues Hey there! Let's work through this error and get your program running properly. That NameError: name 'flight_miles' is not defined is happening because your calculate_flight_miles_earned function doesn't return the calculated value—but there are a few other small bugs we need to fix too. Let's break it down step by step:
Key Issues & Fixes
1. calculate_flight_miles_earned doesn't return a value
Right now, you calculate flight_miles inside the function, but never send it back to the main code. That means when you do flight_miles = calculate_flight_miles_earned(flights_purchased), flight_miles ends up as None, which causes errors later. Add a return flight_miles at the end of the function:
def calculate_flight_miles_earned(flights_purchased): flight_miles = 0 if flights_purchased == 0: flight_miles = 100 elif flights_purchased == 1: flight_miles = 5 elif flights_purchased == 2: flight_miles = 15 elif flights_purchased == 3: flight_miles = 30 elif flights_purchased > 3: flight_miles = 60 return flight_miles # Add this line!
2. final_evaluation has incorrect parameters & undefined variables
Your function definition includes output_flight_tier as a parameter (which you don't use), and when you call it in main(), you reference flights—a variable that doesn't exist. Let's fix the function to use flights_purchased (since your evaluation is about flight count) and remove the unused parameter:
def final_evaluation(flights_purchased): evaluation = "" if flights_purchased < 15: evaluation = "You are not a frequent flyer" elif flights_purchased > 15: evaluation = "You are a frequent flyer" # Handle the edge case for exactly 15 flights else: evaluation = "You are a frequent flyer" return evaluation
Then update the call in main() to:
evaluation = final_evaluation(flights_purchased)
3. output_point uses wrong parameters & undefined variables
This function is set up to accept four functions as parameters, but you actually need to pass the calculated values (flights_purchased, flight_miles, evaluation). Rewrite the function to use these values instead:
def output_point(flights_purchased, flight_miles, evaluation): print(f"The number of flights you have purchased is {flights_purchased}") print(f"The total # of miles earned from flights you have purchased is {flight_miles}") print(f"The final evaluation is: {evaluation}")
And update the call in main() to:
output_point(flights_purchased, flight_miles, evaluation)
4. get_flights_purchased has a broken loop condition
The line while flights_purchased is not int: doesn't work—flights_purchased starts as 0 (an integer), so the loop never runs. Instead, use an infinite loop that breaks only when valid input is received:
def get_flights_purchased(): while True: try: flights_purchased = int(input("Please enter the number of flights that you purchased this month: ")) return flights_purchased except ValueError: print('That is not a number. Please provide a whole number.')
5. Minor Tier Label Consistency
You have duplicate "MVP GOLD" labels for 30 miles and over 30 miles. To make it clearer, you could adjust the highest tier, like:
elif flight_miles > 30: print("You are in the MVP GOLD ELITE airline tier!")
Full Corrected Code
Here's the complete fixed program:
def get_flights_purchased(): while True: try: flights_purchased = int(input("Please enter the number of flights that you purchased this month: ")) return flights_purchased except ValueError: print('That is not a number. Please provide a whole number.') def calculate_flight_miles_earned(flights_purchased): flight_miles = 0 if flights_purchased == 0: flight_miles = 100 elif flights_purchased == 1: flight_miles = 5 elif flights_purchased == 2: flight_miles = 15 elif flights_purchased == 3: flight_miles = 30 elif flights_purchased > 3: flight_miles = 60 return flight_miles def output_flight_tier(flight_miles): if flight_miles == 0: print("You haven't flown with us yet, so you don't have a flight tier.") elif flight_miles == 5: print("You are in the economy airline tier") elif flight_miles == 15: print("You are in the MVP airline tier") elif flight_miles == 30: print("You are in the MVP GOLD airline tier") elif flight_miles > 30: print("You are in the MVP GOLD ELITE airline tier!") def final_evaluation(flights_purchased): evaluation = "" if flights_purchased < 15: evaluation = "You are not a frequent flyer" else: evaluation = "You are a frequent flyer" return evaluation def output_point(flights_purchased, flight_miles, evaluation): print(f"The number of flights you have purchased is {flights_purchased}") print(f"The total # of miles earned from flights you have purchased is {flight_miles}") print(f"The final evaluation is: {evaluation}") def main(): flights_purchased = get_flights_purchased() flight_miles = calculate_flight_miles_earned(flights_purchased) output_flight_tier(flight_miles) evaluation = final_evaluation(flights_purchased) output_point(flights_purchased, flight_miles, evaluation) main()
Now when you run this, it should accept input, calculate miles correctly, display your tier, and give you the frequent flyer evaluation without any NameErrors!
内容的提问来源于stack exchange,提问作者user9825297

