Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中计算航空公司排名-name错误:未定义名称_Python_Python 3.x - Fatal编程技术网

在Python中计算航空公司排名-name错误:未定义名称

在Python中计算航空公司排名-name错误:未定义名称,python,python-3.x,Python,Python 3.x,我是Python新手,我的编程课需要一些帮助。我想我几乎已经记下来了,但是当我试着运行我的主模块时,我一直得到“NameError:name'flight_miles'未定义”。任何帮助都将不胜感激!下面是我的程序 def get_flights_purchased(): flights_purchased = 0 while flights_purchased is not int: try: flights_purchased = in

我是Python新手,我的编程课需要一些帮助。我想我几乎已经记下来了,但是当我试着运行我的主模块时,我一直得到“NameError:name'flight_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()
def get_flights_purchased():
购买的航班=0
虽然购买的航班不完整:
尝试:
flights\u purchased=int(输入(“请输入您本月购买的航班数量”))
打破
除值错误外:
打印('这不是一个数字。请提供一个整数')
购买回程机票
def计算获得的航班里程(购买的航班):
飞行里程=0
如果购买的航班=0:
飞行里程=100
elif购买的航班=1:
飞行英里数=5
elif购买的航班=2:
飞行里程=15
elif购买的航班=3:
飞行里程=30
elif购买的航班>3次:
飞行里程=60
def输出飞行层(飞行里程):
如果飞行里程=0:
打印(“您尚未与我们一起飞行,因此您没有飞行层。”)
elif飞行里程==5:
打印(“您在经济型航空公司级别”)
elif飞行里程==15:
打印(“您处于MVP航空公司层级”)
elif飞行里程==30:
打印(“您处于MVP黄金航空公司层级”)
elif飞行里程>30:
打印(“您在MVP黄金级别!”)
def最终评估(航班、输出航班层):
evaluation=“”
如果航班<15:
evaluation=“您不是常客”
elif航班>15次:
evaluation=“你是常客”
回报评估
def输出点(获取购买的航班、计算获得的航班里程、输出航班层级、最终评估):
打印(“您购买的航班数为”,购买的航班数为)
打印(“您购买的航班总里程为”,航班)
打印(“最终评估为”,最终评估)
def main():
购买的航班=0
飞行里程=0
evaluation=“”
购买的航班=获取购买的航班()
飞行里程=计算获得的飞行里程(购买的航班)
输出飞行层(飞行里程)
评估=最终评估(航班、输出航班层)
输出点(获取购买的航班、计算获得的航班、里程、输出航班、最终评估)
main()

您错误地调用了第二个子程序。 而不是:

flight_miles(calculate_flight_miles_earned)
应该是:

calculate_flight_miles_earned(flights_purchased)
一般来说,调用函数时,函数的形式应为:

return_value = function(parameter1, parameter2...)
您的代码还有许多其他实例发生了此错误,但使用上述格式应该可以解决这些问题。如果没有返回值,只需调用

function(parameters)

再加上random.george 11的答案,您从未在
main
中定义过“
flight\u miles
”,只在
calculate\u flight\u miles
中定义过。在
output\u flight\u tier=flight\u miles
main
中再次调用该值时,该值不会结转。返回值不在功能范围内

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

您的主要错误仍然是由于错误的函数调用造成的

例如,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()
def get_flights_purchased():
购买的航班=-1
#“int”是一种类型,因此您购买的航班不等于“int”
而购买的航班==-1:
尝试:
flights\u purchased=int(输入(“请输入您本月购买的航班数量”))
除值错误外:
打印('这不是一个数字。请提供一个整数')
购买回程机票
def计算获得的航班里程(购买的航班):
飞行里程=0
如果购买的航班=0:
飞行里程=100
elif购买的航班=1:
飞行英里数=5
elif购买的航班=2:
飞行里程=15
elif购买的航班=3:
飞行里程=30
elif购买的航班>3次:
飞行里程=60
#临界线-如果不返回此值,飞行里程将不在主数据中
回程飞行里程
def输出飞行层(飞行里程):
如果飞行里程=0:
打印(“您尚未与我们一起飞行,因此您没有飞行层。”)
elif飞行里程==5:
打印(“您在经济型航空公司级别”)
elif飞行里程==15:
打印(“您处于MVP航空公司层级”)
elif飞行里程==30:
打印(“您处于MVP黄金航空公司层级”)
elif飞行里程>30:
打印(“您在MVP黄金级别!”)
def最终评估(航班、输出航班层):
evaluation=“”
#在这两种情况中的一种情况下需要一个等号,如果航班=15,则评估将保留“”
#例如,如果航班15:
evaluation=“你是常客”
回报评估
#子程序的参数必须与子程序中使用的值匹配
#参数应该是值,而不是其他子程序
def输出点(购买的航班、航班、最终评估):
打印(“您购买的航班数为”,购买的航班数为)
打印(“您购买的航班总里程为”,航班)
打印(“最终评估为”,最终评估)
def main():
购买的航班=0
飞行里程=0
evaluation=“”
购买的航班=获取购买的航班()
飞行里程=计算获得的飞行里程(购买的航班)
输出飞行层(飞行里程)
#将航班改为飞行里程
评估=最终评估(飞行里程,起飞)