0

I created two strings: edibles and vz. I wish to create a loop where the user is required to 1) input a food 2) be told if I am allergic to it. If the user types a name from the edibles string, they're told of an allergy. If the user types a name from the vz string, there is no allergy. I also include an option for a food item that is not included in either string.

At present, my code treats the items individually. I need assistance to figure out how to match input to the string without needing to add an elif line for every item.

edibles = ["ham", "jachnun", "tiger", "ostrich head","eggs","nuts", "spam"]
vz = ["gefilte fish", "liver", "chrain", "sushi", "cholent"]
edv = vz + edibles

while True:
    sval = input('Enter a food: ')
    if sval == "spam":
        print("I'm allergic to: " + sval + " Get it away from me")
    elif sval == "jachnun":
        print("I'm allergic to: " + sval + " Get it away from me")
    elif sval == "gefilte fish":
        print("I am not allergic to " + sval)
    elif sval == "done":
        break
    else:
        print(sval + " is an unfamiliar item" )
print('Thank you for respecting my allergies')
Sheldore
  • 37,862
  • 7
  • 57
  • 71
David L
  • 47
  • 1
  • 5

1 Answers1

0

I don't know why you need edv for. One could probably use some data frames for this problem too. Here is a solution as you asked to get rid of redundant elif's.

while True:
    sval = input('Enter a food: ')
    if sval in edibles:
        print("I'm allergic to: " + sval + " Get it away from me")
    elif sval in vz:
        print("I am not allergic to " + sval)
    elif sval == "done":
        break
    else:
        print(sval + " is an unfamiliar item" )
Sheldore
  • 37,862
  • 7
  • 57
  • 71