1

How can I find which position on a list an input is, without using 'if' statements? My current code is below. I want to remove the if statements, so that when a breed is inputted, the computer outputs "Great choice!" then separately outputs the price, in as compact code as possible. I need something which finds which value on a list an input is, the prints the corresponding position from ANOTHER list.

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.")
Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
Julian Ion
  • 11
  • 1
  • 2
    Use a dictionary. – Michael H. Sep 14 '17 at 18:20
  • 2
    Possible duplicate of [Finding the index of an item given a list containing it in Python](https://stackoverflow.com/questions/176918/finding-the-index-of-an-item-given-a-list-containing-it-in-python) – Bill the Lizard Sep 14 '17 at 18:20
  • adding to @BilltheLizard documentation for the dictionary api in python [dict api](https://docs.python.org/3.5/library/stdtypes.html#mapping-types-dict) – Andrei Sep 14 '17 at 18:22

2 Answers2

3

Using a dictionary:

dog_breed_list = ["daschund", "chihuahua", "French boxer",
                  "Jack Russell", "poodle"]

dog_price_list = [350, 640, 530, 400, 370]

dictionary = {dog_breed_list[n]: dog_price_list[n]
              for n in range(len(dog_breed_list))}

dog_choice = input("Welcome to the Pet Shop. \nWhich is your breed choice? ")

if dog_choice in dictionary:
    print("Great choice! This breed costs £"+str(dictionary[dog_choice])+".")
Michael H.
  • 3,323
  • 2
  • 23
  • 31
  • Up voted for using a dict is the obvious choice here but even keeping the two lists is useless - just build the dict directly or at least from a list of breed=>price tuples. – bruno desthuilliers Sep 14 '17 at 18:52
  • That's a good point. I did it this way because it meant the minimum effort with the code of OP being given ;-) – Michael H. Sep 14 '17 at 19:03
1

If you must uses a list for this, you can use the .index() function.

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?")

try:
    dog_price = dog_price_list[dog_breed_list.index(dog_choice)]
    print("Great choice! This breed costs £{}.".format(dog_price))

except ValueError:
    print('That dog is not found in the list.')

The try-except block is because .index() throws a value error if it doesn't find what it's looking for in that list.

Eric Ed Lohmar
  • 1,832
  • 1
  • 17
  • 26