我无法弄清楚为什么这段代码不会计入20个。我需要创建一个可以计算20张票的代码,但如果购买10张或更多票,则会给出10%的折扣。这是我到目前为止的代码
start = 1
end = 21
increment = 1
TicketPrice = float(input("Enter the price of one ticket: "))
while TicketPrice <= 0:
print("The price of one ticket must be greater than zero. Try again!")
TicketPrice = float(input("Enter the price of one ticket: "))
def main():
print('Tickets\t Total Price')
for Tickets in range (start, end, increment):
Total = TicketPrice * Tickets
while Tickets >= 10:
Total = 0.90 * Tickets * TicketPrice
print(Tickets, '\t ', Total)
main()
编辑:我将while循环更改为if并且解决了问题,但我现在需要将代码限制为2个小数点而不向上或向下舍入
答案 0 :(得分:0)
for Tickets in range (start, end, increment):
Total = TicketPrice * Tickets
if Tickets >= 10:
Total *= 0.90
正如您最初编写的那样,Tickets为10时的迭代,同时循环播放并重新计算10%的折扣。
答案 1 :(得分:0)
def get_ticket_price():
price = float(input('Enter the price of one ticket: '))
if price <= 0:
print('The price of one ticket must be greater than zero. Try again!')
return get_ticket_price()
return price
ticket_price = get_ticket_price()
total = 0
for ticket_count in range (1, 21):
price = ticket_price
if ticket_count >= 10:
price = ticket_price - (ticket_price * .1)
total = total + price
print(total)