【发布时间】:2021-02-07 03:39:27
【问题描述】:
下面是关于任务的基于文本的 Python 实现算法。任务是,
“车票可以为单人或一组乘客购买。购买时,请检查所需的上下山列车的车票数量。如果有车票,计算总数价格包括任何团体折扣。更新屏幕显示和总计数据。"
这是此任务的描述。
“一条电动山地铁路每天有四趟回程。每趟火车分别在 9、11、13 和 15 点上山。火车在 10、12、14 点从山顶返回,然后16.每列6节车厢,80个座位。旅客只能购买来回车票,所有车票必须在乘车当天购买。上车费$25,上车费$25 10 至 80 人(含 10 至 80 人)的团体,每 10 位乘客免费获得一张,前提是他们一起旅行。乘客必须在购买车票时预订回程火车和出发火车。乘客可以返回在下山的下一趟火车或稍后的火车上。从山顶出发的最后一趟火车上有两个额外的车厢。
大屏幕上显示火车时间,以及每辆火车的剩余车票数量。每次订票时,显示都会更新。当火车已满时,会显示“已关闭”字样,而不是可用票数
当 15 点钟的票为 0 时,当我尝试预订 16 点钟的票时,因为有额外的票可用,它要求输入适当数量的票,而不是让我预订票。
你能告诉我为什么会出现这个错误吗?请问您能修复错误吗?提前感谢您!
仅供参考,我是该领域的初学者,正在努力学习基础知识。
train_up_time = [9,11,13,15]
train_down_time = [10,12,14,16]
train_up_ticket =[480]*4
train_down_ticket =[480,480,480,640]
train_up_money =[0.00]*4
train_down_money = [0.00]*4
cost = 25.00
index_up = 0
index_down = 0
ticket_cost = 0
ticket_counter = 0
print("The available time and tickets for the trip Up to the mountain ")
print("Available time\t\t Available tickets \t\t Total money of the trip / $ \n")
for i in range(0, 4):
print(train_up_time[ i ], "\t\t\t", train_up_ticket[ i ], "\t\t\t", train_up_money[ i ])
print("The available time and tickets for the trip Down from the mountain ")
print("Available time\t\t Available tickets \t\t Total money of the trip / $ \n")
for i in range(0, 4):
print(train_down_time[ i ], "\t\t\t", train_down_ticket[ i ], "\t\t\t", train_down_money[ i ])
selling_ticket = int(input("Would you like buy ticket for trip ? Enter 1 else -1. : " ))
while selling_ticket == 1:
time_up = int(input("What time would you like to buy for 9 11 13 15? : "))
while time_up != 9 and time_up != 11 and time_up != 13 and time_up != 15 :
print("Error! please select the appropraite time from the available.")
time_up = int(input("What time would you like buy for 9 11 13 15? : "))
time_down = int(input("What time would you like to return 10 12 14 16? : "))
while time_up > time_down or ( time_down != 10 and time_down != 12 and time_down != 14 and time_down != 16) :
print("Error! please select the appropraite time (you must not select the time below the departure)." )
time_down = int(input("What time would you like to return 10 12 14 16? : "))
for count in range(0,4):
if time_up == train_up_time[ count ]:
index_up = count
if time_down == train_down_time[ count]:
index_down = count
for i in range(0, 4):
if train_up_ticket [ index_up ] == "CLOSED":
train_up_ticket [ index_up ] = 0
if train_down_ticket[ index_down ] == "CLOSED" :
train_down_ticket[ index_down ] = 0
num_ticket = int(input("How many tickets would you like to buy? : "))
while num_ticket > train_up_ticket[ index_up ] or num_ticket > train_down_ticket [ index_down ] :
print("Error! Please recheck the availability of the trian ticket")
num_ticket = int(input("How many tickets would you like to buy? : "))
print("Every 10th passenger is FREE!")
train_up_ticket [ index_up ] = train_up_ticket [ index_up ] - num_ticket
train_down_ticket[ index_down ] = train_down_ticket[ index_down ] - num_ticket
if num_ticket >= 10:
ticket_cost = cost * (num_ticket - (num_ticket/10))
else:
ticket_cost = cost * num_ticket
print("Your trip cost: ", "$", ticket_cost)
print("You need to pay for both ways")
print("Therefore, your total trip cost including the discount is : ", "$", ticket_cost*2 )
train_up_money[ index_up ] = train_up_money[ index_up ] + ticket_cost
train_down_money[ index_down ] = train_down_money[ index_down ] + ticket_cost
if train_up_ticket [ index_up ] == 0:
train_up_ticket [ index_up ] = "CLOSED"
if train_down_ticket[ index_down ] == 0:
train_down_ticket[ index_down ] = "CLOSED"
print("The available time and tickets for the trip Up to the mountain ")
print("Available time\t\t Available tickets \t\t Total money of the trip / $ \n")
for i in range(0, 4):
print(train_up_time[ i ], "\t\t\t", train_up_ticket[ i ], "\t\t\t", train_up_money[ i ])
print("The available time and tickets for the trip Down from the mountain ")
print("Available time\t\t Available tickets \t\t Total money of the trip / $ \n")
for i in range(0, 4):
print(train_down_time[ i ], "\t\t\t", train_down_ticket[ i ], "\t\t\t", train_down_money[ i ])
selling_ticket = int(input("Would you like buy ticket for trip ? Enter 1 else -1. : " ))
【问题讨论】:
-
使用 breakpoint() 调用。这样快得多。
-
添加触发此错误的特定输入集
标签: python algorithm error-handling