Home > Back-end >  if user input is an element of a list
if user input is an element of a list

Time:04-27

I think there must be a simple solution to this, but I can't find it. If user input is equal to an element of a list, then the if statement should evaluate to true:

animalslist = ['bear', 'giraffe', 'wolf', 'lion', 'rabbit', 'monkey', 'scorpion']             
animalnumber = int(input("\nEnter a number between 0 and 6 inclusive.\n"))

print "You have summoned a %s..." % animalslist[animalnumber]

if animalslist[animalnumber] == "scorpion" or "wolf" or "lion" or "bear":
    #or perhaps using the integer - if animalslist[animalnumber] == 0 or 2 or 3 or 5:
    print "That is a vicious predator. You = dinner. The End.\n\n"

else:
    ...

CodePudding user response:

You can use another list. Example:

animalslist = ['bear', 'giraffe', 'wolf', 'lion', 'rabbit', 'monkey', 'scorpion']   
predatorlist = ["scorpion", "wolf", "lion", "bear"]
if animalslist[animalnumber] in predatorlist:
    print "That is a vicious predator. You = dinner. The End.\n\n"

CodePudding user response:

You could do something like this:

animalslist = ['bear', 'giraffe', 'wolf', 'lion', 'rabbit', 'monkey', 'scorpion']             
animalnumber = int(input("\nEnter a number between 0 and 6 inclusive.\n"))
predators = {"scorpion", "wolf", "lion", "bear"}

print "You have summoned a %s..." % animalslist[animalnumber]

if animalslist[animalnumber] in predators:
    print "That is a vicious predator. You = dinner. The End.\n\n"
else:
    ...

CodePudding user response:

I hope I will explain the problem to you.

You problem in this line if animalslist[animalnumber] == "scorpion" or "wolf" or "lion" or "bear": Why?

In python, this looks like this.

if animalslist[animalnumber] == "scorpion" or "wolf" or "lion" or "bear":

In the above line python first, check 
animalslist[animalnumber] == "scorpion" # This could be true or false.

if this is false then python move to next 

Now, the python does not check.

animalslist[animalnumber] == "wolf"
but it is only checking "wolf" which is true (Empty string and 0 returns false and string with the length of 1 or more is return True)

if the python code gets True then it executes the  line inside if block
Solution:

There are multiple ways to do that 


First one:

@Vincent way (Suggested)
animalslist = ['bear', 'giraffe', 'wolf', 'lion', 'rabbit', 'monkey', 'scorpion']   
predatorlist = ["scorpion", "wolf", "lion", "bear"]
if animalslist[animalnumber] in predator list:
    print "That is a vicious predator. You = dinner. The End.\n\n"

or 


animalslist = ['bear', 'giraffe', 'wolf', 'lion', 'rabbit', 'monkey', 'scorpion']   
predatorlist = ["scorpion", "wolf", "lion", "bear"]
if animalslist[animalnumber] == "scorpion" or animalslist[animalnumber] == "wolf" or animalslist[animalnumber] == "lion" or animalslist[animalnumber] == "bear":
    print "That is a vicious predator. You = dinner. The End.\n\n"
else:
    ...

CodePudding user response:

An alternative is to restructure your data, knowing the usage you have for it. This is not dissimilar to @JhanzaibHumayun's answer in terms of semantics, but is another way to think about it:

animals = {
  'bear': True,
  'giraffe': False,
  'wolf': True,
  'lion': True,
  'rabbit': False,
  'monkey': False, 
  'scorpion': True,
}

choice = int(input(f"\nEnter a number between 0 and {len(animals)} inclusive.\n"))
summoned = [*animals][choice]
print f"You have summoned a {summoned}..."
if animals.get(summoned):
    print f"Summoned is a vicious predator. You = dinner. The End. \n\n"

This relates the data about animals and whether they're a predator or not (true/false) in a single data structure. This is no more or less computationally expensive than storing it in two lists, and probably saves a few licks of memory. You could take it a step further by storing additional information beyond their predator nature, though at that point you might want to think about a dataclass.

  • Related