4 回答

TA貢獻(xiàn)1850條經(jīng)驗(yàn) 獲得超11個(gè)贊
您的代碼有點(diǎn)難以閱讀且難以維護(hù)我建議,當(dāng)您想退出時(shí)將“while 循環(huán)”更改為無(wú)限循環(huán),只需打破循環(huán),我更喜歡在詢問(wèn)選項(xiàng)之前顯示菜單。
您可以像這樣更改代碼:
def display_menu():
print("1. Add a new item to shopping list")
print("2. Remove an item")
print("3. Print Shopping List Items")
print("0. Exit")
return int(input('Enter an Options (0~3):'))
while True:
option = display_menu()
if option == 1:
item = input('enter the item : ')
qnty = int(input('Enter the Quantitiy for the item : '))
Shoping_list[item] = qnty
elif option == 2:
for item in Shoping_list:
print(item, ':', Shoping_list[item])
item = input('Enter the item you want to Remove : ')
del(Shoping_list[item])
elif option == 3:
for item in Shoping_list:
print(item, ':', Shoping_list[item])
elif option == 0:
print('shopping list is close')
break # Exit menu
else:
print('you didnt enter a valid number ')

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超6個(gè)贊
太棒了,非常感謝你們!我是 python 的新手,這個(gè)信息非常有幫助
def display_menu():
print("1. Add a new item to shopping list")
print("2. Remove an item")
print("3. Print Shopping List Items")
print("0. Exit")
return int(input('Enter an Options (0~3):'))
while True:
option = display_menu()
if option == 1:
item = input('enter the item : ')
qnty = int(input('Enter the Quantitiy for the item : '))
Shoping_list[item] = qnty
elif option == 2:
for item in Shoping_list:
print(item, ':', Shoping_list[item])
item = input('Enter the item you want to Remove : ')
del(Shoping_list[item])
elif option == 3:
for item in Shoping_list:
print(item, ':', Shoping_list[item])
elif option == 0:
print('shopping list is close')
break # Exit menu
else:
print('you didnt enter a valid number ')
Ps喜歡帶有您可以調(diào)用的功能的選項(xiàng)

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超6個(gè)贊
在每個(gè) if 語(yǔ)句中,在末尾插入 Options = 0。由于您的 while 循環(huán)取決于不為 0 的選項(xiàng)。將其重置為 0 允許用戶選擇另一個(gè)選項(xiàng)。
while Options != 0:
if Options == 1:
item = input('enter the item : ')
qnty = int(input('Enter the Quantitiy for the item : '))
Shoping_list[item] = qnty
Options = 0
另外,作為提示,請(qǐng)確保您的拼寫(xiě)和語(yǔ)法準(zhǔn)確無(wú)誤,并且間距保持一致。它使其他人更容易閱讀您的代碼。
這是正確的 if 循環(huán)的工作示例。用戶可以用 if 循環(huán)修改字典,并且可以一個(gè)接一個(gè)地運(yùn)行它們。
Shoping_list = {}
while True:
Options = int(input('Enter an Options :'))
while Options != 0:
if Options == 1:
item = input('enter the item : ')
qnty = int(input('Enter the Quantitiy for the item : '))
Shoping_list[item] = qnty
Options = 0
elif Options == 2:
for item in Shoping_list:
print(item, ':', Shoping_list[item])
item = input('Enter the item you want to Remove : ')
del(Shoping_list[item])
Options = 0
elif Options == 3:
for item in Shoping_list:
print(item, ':', Shoping_list[item])
Options = 0
elif Options != 0:
print('you didnt enter a valid number ')
else:
print('shopping list is close')

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊
Options = int(input('Enter an option'))在 while 循環(huán)中插入第一條語(yǔ)句。
while options!=0:
Options = int(input('Enter an option'))
.
.
.
.
- 4 回答
- 0 關(guān)注
- 126 瀏覽
添加回答
舉報(bào)