如何在不使用'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