我该如何循环此代码?

时间:2016-02-25 20:12:53

标签: python

price = int(input('Enter price. '))
quan = int(input('Enter quantity. '))
total = price*quan
print('The total is ' +str(total))

price2 = int(input('Enter price. '))
quan2 = int(input('Enter quantity. '))
total2 = price2*quan2     

all_totals = total + total2
print('The sum of the totals is ' +str(all_totals))

理想情况下,我不想要价格等等。但我需要程序循环并将子总数加在一起,然后打印整个总数。我是python的新手,感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:4)

grandtotal = 0

while True:
    price = int(input('Enter price. (0 to quit) '))
    if price == 0:
        break
    quan = int(input('Enter quantity. '))
    subtotal = price*quan
    print('The subtotal is %d' % subtotal)
    grandtotal = grandtotal + subtotal

print('The sum of the totals is %d' % grandtotal)