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