我正在为一个学校项目编写这段代码,它是这样的:valid_entries = ['y', 'n', 'hatchback', 'estate', 'saloon']model_options = { 'Hatchback' : 535000, 'Saloon' : 495000, 'Estate' : 625000}total_price = 0model_chosen = input("Which car model do you want to buy?\n> ")while model_chosen not in valid_entries: print("Invalid Input\nTry again") model_chosen = input("Which car model do you want to buy?\n> ")有没有办法可以访问这些值并将其与输入进行比较并将其添加到分数中?
2 回答
ITMISS
TA贡献1871条经验 获得超8个赞
您可以使用以下语法访问该值:dict[key]。例如,在您的代码中,models_options[Estate] 的值为 625000。您实际上并不需要列表 valid_entries。您可以尝试以下代码:
valid_entries = ['y', 'n', 'hatchback', 'estate', 'saloon']
model_options = {
'Hatchback' : 535000,
'Saloon' : 495000,
'Estate' : 625000
}
total_price = 0
model_chosen = input("Which car model do you want to buy?\n> ")
while model_chosen not in model_options:
print("Invalid Input\nTry again")
model_chosen = input("Which car model do you want to buy?\n> ")
total_price+=model_options[model_chosen]
print(total_price)
慕桂英546537
TA贡献1848条经验 获得超10个赞
值为 model_options[model_chosen],你到底想做什么?你能做到吗:total += model_options[model_chosen]。
添加回答
举报
0/150
提交
取消