再次询问用户或退出循环



我试图询问用户在循环中选择汽水的输入,但我希望能够询问用户想要购买什么汽水,然后循环和再次询问用户或退出这就是我需要帮助的问题所在。

我已经尝试将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('Sodalistn========')  
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),如果指定的键不存在,可以指定返回一个值。[我。在你的情况下初始化为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('Sodalistn========')  

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

输出: -

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('Sodalistn========')  
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()
输出:

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()函数:

def single_bottles():
# Your code
single_bottles()

也可以在不相关的输入上捕获精确异常(KeyError):

try:        
total += prices[inp]      
except KeyError: 
print('Irrelevant input. Exiting.')
break  

最新更新