简体   繁体   English

在 while 循环中,您如何保存数据以便在循环结束时将 output 输入所有数据,而不仅仅是在下一个 while 循环中重置

[英]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)

im trying to make it so this command acts like a group ordering dinner.我正在努力做到这一点,所以这个命令就像一群人点餐一样。 if there is two people in the group it will take in those two inputs and them print both together.如果该组中有两个人,它将接收这两个输入并将它们一起打印。 to specify how many people in the group is just how many times the loop runs.指定组中有多少人就是循环运行的次数。 hope this makes sense希望这是有道理的

#code #代码

 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")

This does what you are trying to do:这就是你想要做的:

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: 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

If I get your problem properly and your problem is that when while loop ends working list bottles is not you wish it to be如果我正确地解决了你的问题,而你的问题是当 while 循环结束时工作列表bottles不是你希望的
then it's maybe because you rewrite variable bottle 3 times each while loop iteration instead of adding anything to it那么这可能是因为你每次 while 循环迭代重写变量bottle 3次而不是向它添加任何东西
so instead of this bottles = [int(howmany) for _ in range(howmany)] you'd try something like this for _ in range(howmany): bottles.append(int(howmany))所以而不是这个bottles = [int(howmany) for _ in range(howmany)]你会尝试这样的东西for _ in range(howmany): bottles.append(int(howmany))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM