2016-02-25 96 views
-1
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)) 

理想情况下,我不想有price2等,但我需要程序来循环和加起来的小计,然后打印什么是总的。我是新来的python和任何帮助表示赞赏。谢谢!我该如何循环这段代码?

回答

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) 
+0

非常感谢!我现在知道了。我感谢您的帮助! –

+0

upvoted。既然你在那里,也许,建议更好地使用'print' – Pynchia