Python 最终总结计算不正确

Python 最终总结计算不正确,python,if-statement,variables,while-loop,Python,If Statement,Variables,While Loop,该计划的作用:该计划是一个机票预订系统,在该系统中,座位已预订,每次预订都是在一个会议中,最终摘要必须将所有会议摘要相加,并给出最终数字 问题:我面临的问题是,最终的总结计算不正确,我想知道是否有人可以建议我,为什么它计算不正确。所有的代码都是必需的,我不能把问题缩短来解释问题 adult_price = 120 child_price = 60 availble_seats = 152 t_adult_s_s = 0 t_child_s_s = 0 t_total_s = 0 seats_l

该计划的作用:该计划是一个机票预订系统,在该系统中,座位已预订,每次预订都是在一个会议中,最终摘要必须将所有会议摘要相加,并给出最终数字

问题:我面临的问题是,最终的总结计算不正确,我想知道是否有人可以建议我,为什么它计算不正确。所有的代码都是必需的,我不能把问题缩短来解释问题

adult_price = 120
child_price = 60
availble_seats = 152

t_adult_s_s = 0
t_child_s_s = 0
t_total_s = 0
seats_left = 0


# stating values which stay constant through out the code#
while True:
    print("*****************************************")
    print("          Welcome to freezy jet          ")
    print("*****************************************")
    print("We have 152 seats avalible to be booked")
    print("- - - - - - - - - - - - - - - - - - - - ")
    print("Adult tickets are £120 per seat")
    print("Child tickets are £60 per seat")
    print("*****************************************")
    # while True:
    while True:
        try:
            adult_ticket = int(input("How many adult tickets would you like to purchase "))

        except ValueError:
            print("Please try again")
            # if user enters string or float message displays#
            continue
        if adult_ticket < 0:
            print("Please try again to book with postive number")
            # if user tries to enter a minus number this message is displayed#
            continue
        else:
            break
    while True:
        try:
            child_ticket = int(input("How many child tickets would you like to purchase "))

        except ValueError:
            print("Please try again")
            continue
        if child_ticket < 0:
            print("Please try again to book with postive numbers")
            continue
        else:
            break
    total_ticket = adult_ticket + child_ticket
    if total_ticket > 152:
        # if user tries to purchase more than 152 tickets this message is displayed#
        print("We are not able to sell you that many tickets due to there being a limited amount of seats")
        print("")
        print("Please try again by purchasing less tickets")
        print("")
        continue
    else:

        print("- - - - - - - - - - - - - - - - - - - - - - - - -  ")
        print("")
        print("             Session summary       ")
        print("The amount of adult tickets purhcased is", adult_ticket)
        print("The amount of child tickets purchased is", child_ticket)
        total_tickets_purchased = (adult_ticket + child_ticket)
        print("The total tickets purchased is", total_tickets_purchased)
        print("****************************************************")
        adult_cost = adult_price * adult_ticket
        # works out total cost of adult tickets#
        child_cost = child_price * child_ticket
        # works out total cost of child tickets#
        total_cost = adult_cost + child_cost
        print("The total cost of adult ticket is", adult_cost)
        print("The total cost of child tickets is", child_cost)
        print("The total cost of the tickets is", total_cost)
        seats_left = availble_seats - child_ticket - adult_ticket
        # works out how many seats are remaining#
        print("The remaining seats left is", seats_left)
        t_adult_s_s = t_adult_s_s + adult_ticket
        t_child_s_s= t_child_s_s + child_ticket
        t_total_s = t_adult_s_s + t_child_s_s
        seats_left = seats_left - t_total_s
        t_adult =adult_ticket * adult_price
        t_child =child_ticket * child_price
        t_total=t_adult + t_child
    # ask if user wants to continue if they do show final summary if not carry on asking
    question = input("press 's' if you want to stop press any other key to continue ")
    if question == 's':
        break
print("- - - - - - - - - - - - - - - - - - - - - - - - - ")
print("")
print("             final summary       ")
print("The total amount of adult seats sold is", t_adult_s_s)
print("The total amount of adult seats sold is", t_child_s_s)
print("The total amount of seats sold is", t_total_s)
print("***************************************************")
print("Seats left is",seats_left)
print("Total price for adult seats sold is", t_adult)
print("Total price for child seats sold is",t_child)
print("Total price for seats sold is",t_total)
print("END")
成人价格=120
儿童价格=60
可用座位=152
t_成人_s_s=0
t_child_s_s=0
t_总计=0
左座=0
#说明在整个代码中保持不变的值#
尽管如此:
打印(“****************************************************”)
打印(“欢迎来到freezy jet”)
打印(“****************************************************”)
打印(“我们有152个座位可预订”)
打印(“-----”)
打印(“成人票每张120英镑”)
打印(“儿童票每张60英镑”)
打印(“****************************************************”)
#尽管如此:
尽管如此:
尝试:
成人票=int(输入(“您想购买多少张成人票”))
除值错误外:
打印(“请重试”)
#如果用户输入字符串或浮点,则显示消息#
持续
如果成人票<0:
打印(“请再次尝试使用邮寄号码预订”)
#如果用户尝试输入负数,则显示此消息#
持续
其他:
打破
尽管如此:
尝试:
child_ticket=int(输入(“您想购买多少张儿童票”))
除值错误外:
打印(“请重试”)
持续
如果儿童票<0:
打印(“请再次尝试使用正号预订”)
持续
其他:
打破
总票=成人票+儿童票
如果总票证>152:
#如果用户尝试购买超过152张票证,将显示此消息#
打印(“由于座位有限,我们无法向您出售那么多票”)
打印(“”)
打印(“请购买较少的车票重试”)
打印(“”)
持续
其他:
打印(“-------”)
打印(“”)
打印(“会议摘要”)
打印(“购买成人票的金额为”,成人票)
打印(“购买的儿童票金额为”,儿童票)
购票总额=(成人票+儿童票)
打印(“已购买的总票数为”,已购买的总票数)
打印(“*************************************************************************”)
成人票价=成人票价*成人票
#计算成人票的总成本#
儿童票价=儿童票价*儿童票
#计算出儿童票的总成本#
总成本=成人成本+儿童成本
打印(“成人票总成本为”,成人票成本)
打印(“儿童票的总成本为”,儿童票成本)
打印(“票的总成本为”,总成本)
座位左=可用座位-儿童票-成人票
#计算出还有多少座位#
打印(“剩余座位为左”,座位为左)
成人票=成人票+成人票
儿童票=儿童票+儿童票
总数=成人+儿童
左座=左座-总座数
成人票=成人票*成人票价格
儿童票=儿童票*儿童票价格
t_总计=t_成人+t_儿童
#询问用户是否希望继续,如果他们确实显示最终摘要,如果不继续询问
问题=输入(“如果要停止,请按“s”,然后按任何其他键继续”)
如果问题=='s':
打破
打印(“-------”)
打印(“”)
打印(“最终摘要”)
打印(“成人座椅销售总额为”,t_成人)
打印(“出售的成人座椅总数为”,t_child_s)
打印(“售出的座位总数为”,t_total_s)
打印(“****************************************************************************”)
打印(“左座为”,左座为)
打印(“成人座椅销售总价为”,t_成人)
打印(“出售儿童座椅的总价为”,t_child)
打印(“售出座位的总价为”,t_总计)
打印(“结束”)

问题在于,您正在计算成人座椅和儿童座椅的总售价(您发布的代码块第81行和第82行)

这就是您当前正在做的:

t_成人票=t_成人票+成人票
儿童票=儿童票+儿童票
总数=成人+儿童
左座=左座-总座数
#这就是bug开始的地方
成人票=成人票*成人票价格
儿童票=儿童票*儿童票价格
#这就是bug结束的地方
t_总计=t_成人+t_儿童
您将上一节的成人票数量乘以成人票价格,将上一节的成人票数量乘以儿童票价格

相反,您要做的是分别乘以成人票和成人票的数量(从所有以前的会话中累积)

像这样:

t_成人票=t_成人票+成人票
儿童票=儿童票+儿童票
总数=成人+儿童
左座=左座-总座数
#这就是变化的开始
t_成人=t_成人*成人价格
t_child=t_child_s_s*child_价格
#这就是变化的终点
t_总计=t_成人+t_儿童

还有另一个bug,您将从总数中减去购买的座位数大约两次:

    # here you calculate how many seats left
    seats_left = availble_seats - child_ticket - adult_ticket
    print("The remaining seats left is", seats_left)
    t_adult_s_s = t_adult_s_s + adult_ticket
    t_child_s_s= t_child_s_s + child_ticket
    t_total_s = t_adult_s_s + t_child_s_s

    # and here one more time
    seats_left = seats_left - t_total_s
当执行一次代码时,我得到:

...
The remaining seats left is 147
press 's' if you want to stop press any other key to continue s
- - - - - - - - - - - - - - - - - - - - - - - - - 

             final summary       
....
Seats left is 142

adult_price = 120 child_price = 60 availble_seats = 152 t_adult_s_s = 0 t_child_s_s = 0 t_total_s = 0 seats_left = 0 def get_count_sets(name): # while True: while True: try: ticket = int(input("How many %s tickets would you like to purchase " % name)) except ValueError: print("Please try again") # if user enters string or float message displays# continue if ticket < 0: print("Please try again to book with postive number") # if user tries to enter a minus number this message is displayed# continue else: return ticket # stating values which stay constant through out the code# while True: print("*****************************************") print(" Welcome to freezy jet ") print("*****************************************") print("We have 152 seats avalible to be booked") print("- - - - - - - - - - - - - - - - - - - - ") print("Adult tickets are £120 per seat") print("Child tickets are £60 per seat") print("*****************************************") # while True: adult_ticket = get_count_sets("adult") child_ticket= get_count_sets("child") total_ticket = adult_ticket + child_ticket ....