所以我在下面写了一个小程序。它工作得很好,除了一个部分,我似乎无法让它工作。每次有一个或多个座位被占用/订购时,我都试图从原来的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)
每次调用
ticket_check
时,都会创建一个值为10的新general_seats
变量。需要将该变量移到函数调用之外,以便在调用之间保持该变量看起来这是一个属于更大应用程序的方法。如果是这种情况,
general_seats
可能不应该每次重置为10。相反,开放席位的数量(genetal_seats
我认为)应该作为一个变量传递,这样就可以更改并返回。根据其他因素,可以将其设置为全局变量,但这通常不是最佳做法。我希望这就是你要找的。如果我误解了,请告诉我澄清后编辑:如果将它们设置为全局变量,
general_seats = 10
和floor_seats = 4
可以从函数中删除。每次函数运行时,这两行分别将变量重置为10和4相关问题 更多 >
编程相关推荐