简体   繁体   中英

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:

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
then it's maybe because you rewrite variable bottle 3 times each while loop iteration instead of adding anything to it
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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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