我是Python的新手,我可以在编程课上使用一些帮助。我想我差不多了,但是当我尝试运行我的主模块时,我一直得到“NameError:name'level_miles'未定义”。任何帮助将不胜感激!以下是我的计划。
def get_flights_purchased():
flights_purchased = 0
while flights_purchased is not int:
try:
flights_purchased = int(input("Please enter the number of flights that you purchased this month "))
break
except ValueError:
print('That is not a number. Please provide a whole number')
return flights_purchased
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
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 tier!")
def final_evaluation(flights, output_flight_tier):
evaluation = ""
if flights < 15:
evaluation = "You are not a frequent flyer"
elif flights > 15:
evaluation = "You are a frequent flyer"
return evaluation
def output_point(get_flights_purchased, calculate_flight_miles_earned, output_flight_tier, final_evaluation):
print("The number of flights you have purchased is ", flights_purchased)
print(" The total # of miles earned from flights you have purchased is ", flights)
print("The final evaluation is", final_evaluation)
def main():
flights_purchased = 0
flight_miles = 0
evaluation = ""
flights_purchased = get_flights_purchased()
flight_miles = calculate_flight_miles_earned(flights_purchased)
output_flight_tier(flight_miles)
evaluation = final_evaluation(flights, output_flight_tier)
output_point(get_flights_purchased, calculate_flight_miles_earned, output_flight_tier, final_evaluation)
main()
答案 0 :(得分:1)
您错误地调用了第二个子程序。 而不是:
flight_miles(calculate_flight_miles_earned)
它应该是:
calculate_flight_miles_earned(flights_purchased)
通常,当您调用函数时,它应采用以下形式:
return_value = function(parameter1, parameter2...)
您的代码有许多其他已发生此错误的实例,但使用上述格式应解决它们。如果您没有返回值,请致电
function(parameters)
答案 1 :(得分:0)
添加到random.george 11的回答中,你从未真正定义过&#39; flight_miles
&#39;在main
中,仅在calculate_flight_miles_earned
中。当您在main
output_flight_tier = flight_miles
IFS
中再次拨打该值时,该值不会结转
答案 2 :(得分:0)
返回超出了功能范围。
def get_flights_purchased():
flights_purchased = 0
while flights_purchased is not int:
try:
flights_purchased = int(input("Please enter the number of
flights that you purchased this
month "))
break
except ValueError:
print('That is not a number. Please provide a whole number')
return flights_purchased
答案 3 :(得分:0)
您的主要错误仍然是错误的函数调用。
例如,output_point接受一些参数,但在执行时不使用这些参数。
我已对您的代码进行了一些编辑,以便它可以正常工作,因为我认为这比尝试解释每个代码更简单。我在每次编辑之前对该行进行了评论,以便您了解代码出错的位置。
def get_flights_purchased():
flights_purchased = -1
# "int" is a type, so flights_purchased is not going to equal "int"
while flights_purchased == -1:
try:
flights_purchased = int(input("Please enter the number of flights that you purchased this month "))
except ValueError:
print('That is not a number. Please provide a whole number')
return flights_purchased
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
#critical line - if you do not return this value, flight_miles will not be in main
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 tier!")
def final_evaluation(flights, output_flight_tier):
evaluation = ""
#need an equal sign in one of these two conditions, as if flights = 15, evaluation will remain ""
#for example, if flights <= 15 instead of flights < 15
if flights < 15:
evaluation = "You are not a frequent flyer"
elif flights > 15:
evaluation = "You are a frequent flyer"
return evaluation
# the parameters of the subprogram must match the values used in the subprogram
# the parameters should be values, not other subprograms
def output_point(flights_purchased, flights, final_evaluation):
print("The number of flights you have purchased is ", flights_purchased)
print(" The total # of miles earned from flights you have purchased is ", flights)
print("The final evaluation is", final_evaluation)
def main():
flights_purchased = 0
flight_miles = 0
evaluation = ""
flights_purchased = get_flights_purchased()
flight_miles = calculate_flight_miles_earned(flights_purchased)
output_flight_tier(flight_miles)
#changed flights to flight_miles
evaluation = final_evaluation(flight_miles, output_flight_tier)
#changed parameters to match the parameters of output_point
output_point(flights_purchased, flight_miles, evaluation)
main()