我有以下無聲拍賣程式練習的代碼:
bidding = 1
entry_dictionary = {}
entries_list = []
while bidding:
entry_dictionary = {}
name = input("What is your name?\n")
bid = int(input("What's your bid?\n$"))
entry_dictionary["name"] = name
entry_dictionary["bid"] = bid
entries_list.append(entry_dictionary)
print(entries_list)
other_bidders = input("Are there any other bidders? Type 'yes' or 'no'\n")
if other_bidders == "yes":
else:
bidding = 0
entry_list 具有以下格式:
entries_list = [
{
"name": "john",
"bid": 100,
},
{
"name": "Laura",
"bid": 500,
},
]
在entries_list作業正常內列印一個值:
print(entries_list[0]["bid"]) # output is "100"
但是,當我在 for 回圈中的 if 陳述句中參考它時:
max_bid = 0
for entry in entries_list:
if entries_list[entry]["bid"] > max_bid:
print("itworks")
我得到一個型別錯誤:串列索引必須是整數或切片,而不是字典
有什么想法嗎?
uj5u.com熱心網友回復:
由于您已經在回圈 entry_list dict,因此您可以對其進行切片
for entry in entries_list:
if entry["bid"] > max_bid:
print("it works")
應該管用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/347804.html
