繁体   English   中英

再次询问用户或退出循环

[英]Ask user again or exit the loop

我试图询问用户输入以循环选择苏打水,但我希望能够询问用户用户想要购买什么苏打水然后循环并再次询问用户或退出(这是我需要帮助的问题。

我曾尝试将 While True 循环更改为 if 语句,但没有运气......

    def single_bottles():
  prices = {"Coca Cola": 16,    "Pepsi": 14,    "Fanta": 16,     "7up": 14,     "Sprite": 13,    "Mt Dew": 16,    "Rasberrysoda": 11,     "Orangina": 12,     "Zingo": 14,    "Pearsoda": 14,    "Pommac": 16,    "Jaffa": 14}

  total = 0

  print('Sodalist\n========')  
  for keys, values in prices.items():    
    print(f'{keys} : {values} kr')    
    
  while True:      
    inp = input('\nWrite name of the soda you want to buy: ').capitalize()      
    try:        
      total += prices[inp]      
    except:        
      break  
  print(f'Total price : {total}')  

所做的更改:-

(1) .get()方法用于在总dictionary.get(keyname, value)中添加一个value,如果指定的key不存在则返回。 [即在你的情况下初始化为0 ]

(2) 如果有人输入我们字典中没有的苏打名称,用户将通过消息了解!!

(3) 代码将运行直到用户不输入Exit/exit

代码:-

def single_bottles():
    prices = {"Coca Cola": 16,    "Pepsi": 14,    "Fanta": 16,     "7up": 14,     "Sprite": 13,    "Mt Dew": 16,    "Rasberrysoda": 11,     "Orangina": 12,     "Zingo": 14,    "Pearsoda": 14,    "Pommac": 16,    "Jaffa": 14}
    total = 0
    print('Sodalist\n========')  
    
    for keys, values in prices.items():    
        print(f'{keys} : {values} kr')    
    inp=""
    while inp!="Exit":      
        inp=input('\nWrite name of the soda you want to buy or write exit/Exit for billing: ').capitalize()
        total += prices.get(inp,0)
        if not prices.get(inp,0) and inp!="Exit":
            print("Sorry this Soda is not available")
    print(f'Total price : {total}') 

single_bottles()

Output:-

Sodalist
========
Coca Cola : 16 kr
Pepsi : 14 kr
Fanta : 16 kr
7up : 14 kr
Sprite : 13 kr
Mt Dew : 16 kr
Rasberrysoda : 11 kr
Orangina : 12 kr
Zingo : 14 kr
Pearsoda : 14 kr
Pommac : 16 kr
Jaffa : 14 kr

Write name of the soda you want to buy or write exit/Exit for billing: zingo
Write name of the soda you want to buy or write exit/Exit for billing: perry
Sorry this Soda is not available

Write name of the soda you want to buy or write exit/Exit for billing: jaffa
Write name of the soda you want to buy or write exit/Exit for billing: exit
Total price : 28

如果我正确理解你的问题,你必须询问用户是购买更多苏打水还是退出?

如果那时,

def single_bottles():
    prices = {"Coca Cola": 16,    "Pepsi": 14,    "Fanta": 16,     "7up": 14,     "Sprite": 13,    "Mt Dew": 16,    "Rasberrysoda": 11,     "Orangina": 12,     "Zingo": 14,    "Pearsoda": 14,    "Pommac": 16,    "Jaffa": 14}

    total = 0

    print('Sodalist\n========')  
    for keys, values in prices.items():    
        print(f'{keys} : {values} kr')    

    while True:      
        inp = input('\nWrite name of the soda you want to buy/Q to exit: ').capitalize()      
        if inp in prices:       
            total += prices[inp]
        elif inp in ['q', 'q'.upper()]:
            break
        else:
            print("Invalid Input, try again.....")
    print(f'Total price : {total}')  

single_bottles()

Output:

Sodalist
========
Coca Cola : 16 kr
Pepsi : 14 kr
Fanta : 16 kr
7up : 14 kr
Sprite : 13 kr
Mt Dew : 16 kr
Rasberrysoda : 11 kr
Orangina : 12 kr
Zingo : 14 kr
Pearsoda : 14 kr
Pommac : 16 kr
Jaffa : 14 kr

Write name of the soda you want to buy/Q to exit: Fanta

Write name of the soda you want to buy/Q to exit: Zango
Invalid Input, try again.....

Write name of the soda you want to buy/Q to exit: Zingo

Write name of the soda you want to buy/Q to exit: q
Total price : 30

执行此操作的最佳方法是将 While True 更改为其他内容。

如果您希望循环在输入空苏打水名称时停止,那么您可以做的是“While inp”。

但是必须进行此更改:

  #THIS HAS TO BE ADDED: 
  inp = "blank"
  #THIS ALSO NEEDS TO BE HERE INSTEAD: 
  while inp:      
     inp = input('\nWrite name of the soda you want to buy: ').capitalize()      
     try:        
       total += prices[inp]      
     except:        
       continue
  print(f'Total price : {total}')

如果你想在输入 Exit 时制作它:

  while inp.lower() != "exit":      
     inp = input('\nWrite name of the soda you want to buy: ').capitalize()      
     try:        
       total += prices[inp]      
     except:        
       continue
  print(f'Total price : {total}')

你的代码很好。 只需在定义后调用 single_bottles() function 即可:

def single_bottles():
    # Your code
single_bottles()

暂无
暂无

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

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