![](/img/trans.png)
[英]How to break for loop while handling API error so you don't lose all your data
[英]in a while loop how do you hold data so that when the loop ends it will output all data inputed and not just reset on the next while loop
bottles = []
while True:
option = str(input('which option would you like?'))
howmany = int(input('how many would you like?'))
tax = float(.06)
if option == '1':
bottles = [int(howmany) for _ in range(howmany)]
# Multiply each value in bottle by 0.1 & print the sum
total = (sum([(b*.99) for b in bottles]))
print((total*tax)+total)
elif option == '2':
bottles = [int(howmany) for _ in range(howmany)]
# Multiply each value in bottle by 0.1 & print the sum
total = (sum([(b*.79) for b in bottles]))
print((total*tax)+total)
elif option == '3':
bottles = [int(howmany) for _ in range(howmany)]
# Multiply each value in bottle by 0.1 & print the sum
total = (sum([(b*1.09) for b in bottles]))
print((total*tax)+total)
我正在努力做到这一点,所以这个命令就像一群人点餐一样。 如果该组中有两个人,它将接收这两个输入并将它们一起打印。 指定组中有多少人就是循环运行的次数。 希望这是有道理的
#代码
quitoption = str(input('would you like to contiune? yes or quit '))
if quitoption == 'quit':
print('thank you we will have your order right up')
quit()
else: print("contiune")
这就是你想要做的:
bottles = []
tax = float(.06)
total = 0
while True:
option = input('which option would you like? ')
if option == '0':
break
howmany = int(input('how many would you like? '))
option = int(option)
bottles.extend( [option] * howmany )
if option == 1:
total += howmany * .99
elif option == 2:
total += howmany * .79
elif option == 3:
total += howmany * 1.09
print( "You ordered these bottles:", bottles )
print( "Total before tax:", total )
print( "Total after tax:", total * (1+tax))
Output:
which option would you like? 1
how many would you like? 5
which option would you like? 2
how many would you like? 3
which option would you like? 3
how many would you like? 2
which option would you like? 0
You ordered these bottles: [1, 1, 1, 1, 1, 2, 2, 2, 3, 3]
Total before tax: 9.5
Total after tax: 10.07
如果我正确地解决了你的问题,而你的问题是当 while 循环结束时工作列表bottles
不是你希望的
那么这可能是因为你每次 while 循环迭代重写变量bottle
3次而不是向它添加任何东西
所以而不是这个bottles = [int(howmany) for _ in range(howmany)]
你会尝试这样的东西for _ in range(howmany): bottles.append(int(howmany))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.