我有一個包含食物特征的串列。
我想要做的是搜索用戶輸入的食物名稱,然后:如果食物在串列中,則列印有關食物的詳細資訊 如果食物不在串列中,則列印一行通知食物沒有找到
我設法創建了一個 for 回圈來遍歷串列并在找到食物時回傳食物的詳細資訊。如果食物不在串列中,我正在努力做的是回傳單行說“找不到食物”。
我嘗試了一個while回圈來基本上說'當找不到食物時,告訴用戶并再次要求輸入'但這沒有奏效我嘗試過'如果食物不在串列中'列印出宣告,但這似乎列印同一行的迭代次數與未找到食物一樣多,或者即使食物在串列中,它也會列印該行。
這是代碼:
#initialising list
food_list = ['banana', 10, 'f', 'yellow'], ['apple', 12, 'f', 'red'], ['pear', 60, 'f', 'green'], ['mango', 5, 'f', 'yellow'], ['lettuce', 3, 'v', 'green'], ['beans', 20, 'v', 'green'], ['red capsicum', 1, 'v', 'red'], ['corn', 20, 'v', 'yellow']
#requesting input to determine which food to look for
name = input('What food are you looking for? ')
#iterable loop to search through nested lists
for food in food_list:
if name in food:
if food[2] == 'f':
print(f'All about', food[0], '--> FRUIT')
print()
print(f'Name:', food[0])
print()
print(f'Colour: ', food[3])
print()
print(f'Total available: {food[1]}')
elif food[2] == 'v':
print(f'All about', food[0], '--> VEGETABLE')
print()
print(f'Name:', food[0])
print()
print(f'Colour: ', food[3])
print()
print(f'Total available: {food[1]}'
以上用于查找食物和列印詳細資訊。當試圖回傳一條線說找不到食物時,以下內容不起作用。
#initialising list
food_list = ['banana', 10, 'f', 'yellow'], ['apple', 12, 'f', 'red'], ['pear', 60, 'f', 'green'], ['mango', 5, 'f', 'yellow'], ['lettuce', 3, 'v', 'green'], ['beans', 20, 'v', 'green'], ['red capsicum', 1, 'v', 'red'], ['corn', 20, 'v', 'yellow']
#requesting input to determine which food to look for
name = input('What food are you looking for? ')
while name not in food_list:
print('Sorry not found')
name = input('What fruit are you looking for? ')
)
#initialising list
food_list = ['banana', 10, 'f', 'yellow'], ['apple', 12, 'f', 'red'], ['pear', 60, 'f', 'green'], ['mango', 5, 'f', 'yellow'], ['lettuce', 3, 'v', 'green'], ['beans', 20, 'v', 'green'], ['red capsicum', 1, 'v', 'red'], ['corn', 20, 'v', 'yellow']
#requesting input to determine which food to look for
name = input('What food are you looking for? ')
for food in food_list:
if name not in food:
print('Sorry not here')
elif name in food:
if food[2] == 'f':
print(f'All about', food[0], '--> FRUIT')
print()
print(f'Name:', food[0])
print()
print(f'Colour: ', food[3])
print()
print(f'Total available: {food[1]}')
elif food[2] == 'v':
print(f'All about', food[0], '--> VEGETABLE')
print()
print(f'Name:', food[0])
print()
print(f'Colour: ', food[3])
print()
print(f'Total available: {food[1]}')
uj5u.com熱心網友回復:
有很多方法可以實作您想要的行為。這是一個。
#iterable loop to search through nested lists
index = -1
for n,food in enumerate(food_list):
if name in food:
index = n
if index == -1:
print('Sorry not found')
else:
food = food_list[index]
if food[2] == 'f':
print(f'All about', food[0], '--> FRUIT')
print()
elif food[2] == 'v':
print(f'All about', food[0], '--> VEGETABLE')
print()
print(f'Name:', food[0])
print()
print(f'Colour: ', food[3])
print()
print(f'Total available: {food[1]}')
uj5u.com熱心網友回復:
我建議建立一個按名稱鍵入的字典,這樣您就可以簡單name in food_dict地查看您是否有該名稱的食物。
food_dict = {f[0]: f for f in food_list} # {name: food}
food_types = {
'f': "FRUIT",
'v': "VEGETABLE",
}
while True:
name = input('What food are you looking for? ')
if name in food_dict:
break
print("Sorry, not found!")
[name, qty, ftype, color] = food_dict[name]
print(f"All about {name} --> {food_types[ftype]}\n")
print(f"Name: {name}\n")
print(f"Colour: {color}\n")
print(f"Total available: {qty}")
uj5u.com熱心網友回復:
一種選擇是使用在找到食物時更新的標志,然后在回圈之后檢查標志值是否已更改。如果沒有,則意味著您沒有找到食物,因此您列印了宣告。
#initialising list
food_list = ['banana', 10, 'f', 'yellow'], ['apple', 12, 'f', 'red'], ['pear', 60, 'f', 'green'], ['mango', 5, 'f', 'yellow'], ['lettuce', 3, 'v', 'green'], ['beans', 20, 'v', 'green'], ['red capsicum', 1, 'v', 'red'], ['corn', 20, 'v', 'yellow']
#requesting input to determine which food to look for
name = input('What food are you looking for? ')
# FLAG INITIALIZATION
flag = True
#iterable loop to search through nested lists
for food in food_list:
if name in food:
flag = False # HERE YOU CHANGE THE STATE OF THE FLAG
if food[2] == 'f':
print(f'All about', food[0], '--> FRUIT')
print()
print(f'Name:', food[0])
print()
print(f'Colour: ', food[3])
print()
print(f'Total available: {food[1]}')
elif food[2] == 'v':
print(f'All about', food[0], '--> VEGETABLE')
print()
print(f'Name:', food[0])
print()
print(f'Colour: ', food[3])
print()
print(f'Total available: {food[1]}')
# CHECK THE FLAG VALUE
if flag:
print("Food Not Found")
順便提一下,使用dict結構對于管理此類資料很有用
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/430362.html
標籤:Python python-3.x 嵌套列表
