在if-elif-else语句中定义值时打印小计?名称错误问题[Python 2.7]

在if-elif-else语句中定义值时打印小计?名称错误问题[Python 2.7],python,if-statement,undefined,nameerror,Python,If Statement,Undefined,Nameerror,我目前正在做一个项目,作为一个航空公司预订系统。到目前为止,我在计算和显示在if-elif-else循环语句中计算的小计时遇到了一个问题,如果这有意义的话 例如,我目前需要计算座位和行李的小计。下面是我的代码: user_people = int(raw_input("Welcome to Ramirez Airlines! How many people will be flying?")) user_seating = str(raw_input("Perfect! Now what t

我目前正在做一个项目,作为一个航空公司预订系统。到目前为止,我在计算和显示在if-elif-else循环语句中计算的小计时遇到了一个问题,如果这有意义的话

例如,我目前需要计算座位和行李的小计。下面是我的代码:

user_people = int(raw_input("Welcome to Ramirez Airlines!  How many people will be flying?"))
user_seating = str(raw_input("Perfect!  Now what type of seating would your party prefer?"))
user_luggage = int(raw_input("Thanks.  Now for your luggage, how many bags would you like to check in?"))
user_frequent = str(raw_input("Got it.  Is anyone in your party a frequent flyer with us?"))
user_continue = str(raw_input("Your reservation was submitted successfully.  Would you like to do another?"))
luggage_total = user_luggage * 50


import time
print time.strftime("Date and time confirmation: %Y-%m-%d %H:%M:%S")

if user_seating == 'economy':
    seats_total = user_people * 916
    print seats_total

elif user_seating == 'business':
    seats_total = user_people * 2650
    print seats_total

else: 
    print user_people * 5180

print luggage_total

print luggage_total + seats_total
正如我上面所说的,我试图根据用户选择的舱位(经济舱、商务舱和头等舱)和需要托运的行李总数(x金额和50美元)来计算预订机票的总价格

这是我在执行上述代码时收到的错误:

Traceback (most recent call last):
  File "C:/Users/Cory/Desktop/Project 1.py", line 26, in <module>
    print luggage_total + seats_total
NameError: name 'seats_total' is not defined
回溯(最近一次呼叫最后一次):
文件“C:/Users/Cory/Desktop/Project 1.py”,第26行,在
打印行李总数+座位总数
名称错误:未定义名称“座位总数”
如何在if-elif-else语句之外定义seats\u total


非常感谢你

那么,您的变量
seats\u total
将仅为
user\u seath==“economy”
user\u seath==“business”
定义。对于第三种情况,没有定义此类变量。要定义“if-elif-else语句之外的座位总数”,只需将其设置为零,例如,或一些默认值。

哎呀,这是个错误,很抱歉。这是Python 2.7嘿,谢谢你的回复!我理解你刚才所说的,但我如何将座位总数添加到行李总数,然后取决于用户选择的座位类型?所有这些都在if-elif-else语句中。默认值(例如0)将在if语句中被覆盖。好吧,你是说我可以在if语句之前或之后声明seats\u-total=0,这会起作用吗?
seats\u-total=0
必须在if语句之前。不用担心。很高兴我能帮忙。如果是这样,请接受答复。