我有一段时间是真的,所以如果飞机上没有足够的座位你就不能预订航班,但我想回到前一行代码(第8行),因为当我这样做的时候:
# b means bookings, fn means Flight Number
b = 0
maxpass = 68
minpass = 0
fn = "FNO123"
amount = 0
seatsremain = 68 - b
print ("Welcome to Flight Booking Program")
print ("Please enter the flight number you want to book/cancel with? (Case Sensitve)")
flight = input()
while flight != "X":
seatsremain = 68 - b
while True:
if flight == fn:
print ("There are currently", seatsremain, "seats remaining")
break
else:
print ("ERROR")
print ("Not a valid flight number! Remember input is CASE SENSITIVE")
print ("Welcome to Flight Booking Program")
print ("Please enter the flight number you want to book/cancel with? (Case Sensitve)")
flight = input()
print ("Would you like to book or cancel your flight?")
booking = input().lower()
while True:
if b == maxpass and booking == "b" or booking == "book":
print ("Sorry but the flight you want is currently fully booked. Sorry for Inconviniance")
print ("Welcome to Flight Booking Program")
print ("Please enter the flight number you want to book/cancel with? (Case Sensitve)")
flight = input()
else:
break
if booking == "c" or booking == "b" or booking == "book" or booking == "cancel":
if booking == "b" or booking == "book":
print ("How many seats are you booking?")
while True:
try:
amount = int(input())
if amount <1 or amount >seatsremain:
print ("ERROR")
print ("You must book at least 1 seat and not exceed the maximum amount of seats avaliable")
print ("There are currently", seatsremain, "seats remaining")
print ("How many seats are you booking?")
else:
b = b + amount
print ("Your flight has been Booked!")
print ("Welcome to Flight Booking Program")
print ("Please enter the flight number you want to book/cancel with? (Case Sensitve)")
flight = input()
break
except ValueError:
print ("You must enter a valid number!")
else:
if booking == "c" or booking == "cancel":
print ("How many seats are you canceling?")
while True:
try:
amount = int(input())
if amount <1 or amount >b:
print ("ERROR")
print ("You must cancel at least 1 seat and not exceed the minimum amount of seats avaliable")
print ("There are currently", seatsremain, "seats remaining")
print ("How many seats are you cancelling?")
else:
b = b - amount
print ("Your flight has been Cancelled!")
print ("Welcome to Flight Booking Program")
print ("Please enter the flight number you want to book/cancel with? (Case Sensitve)")
flight = input()
break
except ValueError:
print ("You must enter a valid number!")
else:
print ("Error")
print ("Please only type book or cancel")
print("There are", b, "people booked on flight", fn)
不断重复。
以下是代码的其余部分:
{{1}}
答案 0 :(得分:1)
你所要求的并不是100%清楚,但我怀疑你想要这样的东西:
class MyOwnError(Exception):
def __init__(self, message):
self.message = message
def __str__(self):
return repr(self.message)
while flight != "X":
try:
seatsremain = 68 - b
if flight == fn:
print ("There are currently", seatsremain, "seats remaining")
else:
raise MyOwnError("Not a valid flight number!")
# etc...
if amount <1 or amount >b:
print ("ERROR")
print ("You must cancel at least 1 seat and not exceed the minimum amount of seats avaliable")
print ("There are currently", seatsremain, "seats remaining")
print ("How many seats are you cancelling?")
else:
b = b - amount
raise MyOwnError("Your flight has been Cancelled!")
# etc...
except MyOwnError as e:
print (e.message)
print ("Welcome to Flight Booking Program")
print ("Please enter the flight number you want to book/cancel with? (Case Sensitve)")
flight = input()
这里异常强制跳出所有循环,并可以重新提示输入。您可以通过使用嵌套的try
/ except
语句来扩展此想法以处理座位取消循环。