我正在尝试为每辆新车分配一个唯一的值。如何通过d [new_ID] =(新词典项目)或update()方法添加项目?
# Create blank dictionary
d = {}
# The for loop executes until user has filled in each statement
for i in range(5):
brand = str(input("Enter you car brand: "))
model = str(input("Enter you car model: "))
begodometer = str(input("Enter your starting odometer reading: "))
endodometer = str(input("Enter your ending odometer reading: "))
estmpg = str(input("Enter estimated miles per gallon: "))
# Not sure how to add ID key items to dictionary and then use update()
# Print dictionary
print(d)
break
答案 0 :(得分:0)
这是您要找的吗?
# Create blank dictionary
d = {}
# The for loop executes until user has filled in each statement
for i in range(5):
brand = str(input("Enter you car brand: "))
model = str(input("Enter you car model: "))
begodometer = str(input("Enter your starting odometer reading: "))
endodometer = str(input("Enter your ending odometer reading: "))
estmpg = str(input("Enter estimated miles per gallon: "))
#the "ID" is the iteration # but could be a random # if you like
d[i]={"brand":brand,
"model":model,
"begodometer":begodometer,
"endodometer":endodometer,
"estmpg":estmpg}
# Print dictionary
print(d)
每个“记录”都是一本新词典。
干杯!