如何在不使用'if'语句的情况下查找输入列表上的哪个位置? 我目前的代码如下。我想删除if语句,这样当一个品种被输入时,计算机输出“很棒的选择!”然后以尽可能紧凑的代码分别输出价格。我需要找到某个输入列表上的哪个值,并从另一个列表中打印相应的位置。(Python 3.5.2)如何查找列表中的哪个项目是我的输入?
dog_breed_list = ["daschund", "chihuahua", "French boxer", "Jack Russell",
"poodle"]
dog_price_list = [350, 640, 530, 400, 370]
dog_choice = input("Welcome to the Pet Shop. \nWhich is your breed choice?")
if dog_choice == dog_breed_list[0]:
print("Great choice! This breed costs £350.")
elif dog_choice == dog_breed_list[1]:
print("Great choice! This breed costs £640.")
elif dog_choice == dog_breed_list[2]:
print("Great choice! This breed costs £530.")
elif dog_choice == dog_breed_list[3]:
print("Great choice! This breed costs £400.")
使用字典。 – Michael
[查找包含Python的列表中的项目索引]可能的副本(https://stackoverflow.com/questions/176918/finding-the-index-of-an-item-given-a-list-含它合蟒) –
在python加入@BilltheLizard文档字典API [字典API](https://docs.python.org/3.5/library/stdtypes.html#mapping-types-dict) – Andrei