【发布时间】:2023-04-04 22:37:02
【问题描述】:
所以我在下面写了一个小程序。它工作得很好,除了我似乎无法让它工作的一部分。每次购买/订购一个或多个时,我都会尝试从 10 或 4 的原始值中扣除空位。但我看不到能够做到这一点。除此之外,我的代码似乎对我来说效果很好。您能否查看我的代码并帮助我改进。谢谢。
例如,我希望每次用户输入需要的座位数时,都会扣除一般座位的座位数。如果他们需要 2 个座位,应该是 10 - 2,或者如果他们需要地板座位,应该是 4 - 2
def ticket_check(section, seats):
sections = "general \n floor"
general_seats = 10
floor_seats = 4
total_seats = general_seats + floor_seats
print ("Available sections:",sections)
section = input("Chose a section (G or F): ").capitalize()
if general_seats > 0 or floor_seats > 0:
if section == "G":
print ("Available seats", general_seats)
if general_seats > 0:
general_seat_order = int(input("Choose no. of seats: "))
general_seats = general_seats - general_seat_order
print ("Your seat order has been confirmed")
else:
print ("Sorry, no more general seats available")
elif section == "F":
print ("Available seats",floor_seats)
if floor_seats > 0:
floor_seat_order = int(input("Choose no. of seats: "))
floor_seats = floor_seats - floor_seat_order
print ("Your seat order has been confirmed")
else:
print ("Sorry, No more floor seats available")
else:
print ("Sorry, Section not available")
else:
print ("Pre-sale seats are sold out")
ticket_check("general \n floor", 14)
【问题讨论】:
-
“你能检查我的代码并帮助我改进吗?”如果您能查明导致意外结果的线路,我们将真的感激不尽。在每个阶段,尝试使用
print语句来查看发生了什么。否则,我担心这个问题可能过于宽泛而无法回答。 -
当前输出是多少?期望的输出是什么?
标签: python loops auto-increment