簡體   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)

我正在努力做到這一點,所以這個命令就像一群人點餐一樣。 如果該組中有兩個人,它將接收這兩個輸入並將它們一起打印。 指定組中有多少人就是循環運行的次數。 希望這是有道理的

#代碼

 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM