Using elif statement "print function" not working correctly instead printing if statement's "print function" instead

Question:

I am trying to make a selection tool to pick my next anime, I used the random package to select which one would be the next to watch and this works correctly my issue lies in the following I want to add a description to the selected show, for example, if it picks show b I want to know what it’s about. The current issue is that the print function in the elif statement arent working and instead it keeps choosing the description of the first one.

import random

print("Project Select")
print("")
#for future me
print("Summary Of Project: This Project Has The Goal To Help Select What Anime I Should Watch Next")
print("")
Anime = ["Black Bullet","Princess Connect","Overlord","Date A Live", "Chivalry of a failed knight", "The Detective Is Already Dead",
"Shimoneta", "I'm Quitting Heroing","The Greateast Mage"]

selector = random.choice(Anime)

print(selector)

if("Black Bullet"):
    print("Banana 1")
elif("Princess Connect"):
    print("Watermelon 2")
elif("Overlord"):
    print("Strawberry 3")
elif("Date A Live"):
    print("kiwi 4")
elif("Chivalry Of A Failed Knight"):
    print("apple 5")
elif("The Detective Is Already Dead"):
    print("blueberry 6")
elif("Shimoneta"):
    print("lemon 7")
elif("I'm Quitting Heroing"):
    print("cherry 8")
else:
    print("orange 9")
Asked By: Danny J. Sun

||

Answers:

You’ll need to use the == operator like so:

import random

print("Project Select")
print("")
#for future me
print("Summary Of Project: This Project Has The Goal To Help Select What Anime I Should Watch Next")
print("")
Anime = ["Black Bullet","Princess Connect","Overlord","Date A Live", "Chivalry of a failed knight", "The Detective Is Already Dead",
"Shimoneta", "I'm Quitting Heroing","The Greateast Mage"]

selector = random.choice(Anime)

print(selector)

if selector == "Black Bullet":
    print("Banana 1")
elif selector == "Princess Connect":
    print("Watermelon 2")
elif selector == "Overlord":
    print("Strawberry 3")
elif selector == "Date A Live":
    print("kiwi 4")
elif selector == "Chivalry Of A Failed Knight":
    print("apple 5")
elif selector == "The Detective Is Already Dead":
    print("blueberry 6")
elif selector == "Shimoneta":
    print("lemon 7")
elif selector == "I'm Quitting Heroing":
    print("cherry 8")
else:
    print("orange 9")

Here is why your code didn’t work:

For the ifelif statements, you had:

if("Black Bullet"):
    print("Banana 1")
elif("Princess Connect"):
    print("Watermelon 2")
elif("Overlord"):
    print("Strawberry 3")
elif("Date A Live"):
    print("kiwi 4")
elif("Chivalry Of A Failed Knight"):
    print("apple 5")
elif("The Detective Is Already Dead"):
    print("blueberry 6")
elif("Shimoneta"):
    print("lemon 7")
elif("I'm Quitting Heroing"):
    print("cherry 8")
else:
    print("orange 9")

which was telling Python: "If the boolean value for "Black Bullet" is equal to True, then execute print("Banana 1"), else, if the boolean value of…" and so on.

The only way for a string’s boolean value to be False is when the string is empty, so you get why the code only printed Banana 1.

Answered By: Ann Zen

The issue with this is that your first if statement will ALWAYS evaluate to True, and cause the code indented inside that block to run. Non-empty strings evaluate to True:

>>> bool("")
False
>>> bool("hello")
True

To fix your problem, you will want to check for the equality of selector, which will contain whatever is returned by random.choice(Anime). Here is a simple example:

>>> import random
>>> def foo(lst):
...     rand = random.choice(lst)
...     if rand == 1:
...         print("1 is the element")
...     elif rand == 2:
...         print("2 is the element")
...     elif rand == 3:
...         print("3 is the element")
...
>>> foo([1, 2, 3])
1 is the element
>>> foo([1, 2, 3])
2 is the element
>>> foo([1, 2, 3])
2 is the element
>>> foo([1, 2, 3])
1 is the element
>>> foo([1, 2, 3])
3 is the element
Answered By: binds

Here’s an example of syntax for Python 3.x (Python3 in short). By the way you have typo "Chivalry of a failed knight" should be "Chivalry Of A Failed Knight"

import random

print("Project Select")
print("")
#for future me
print("Summary Of Project: This Project Has The Goal To Help Select What Anime I Should Watch Next")
print("")
Anime = ["Black Bullet", "Princess Connect", "Overlord", "Date A Live", "Chivalry Of A Failed Knight", "The Detective Is Already Dead",
"Shimoneta", "I'm Quitting Heroing", "The Greateast Mage"]

selector = random.choice(Anime)

print(selector)

if selector == "Black Bullet":
    print("Banana 1")
elif selector == "Princess Connect":
    print("Watermelon 2")
elif selector == "Overlord":
    print("Strawberry 3")
elif selector == "Date A Live":
    print("kiwi 4")
elif selector == "Chivalry Of A Failed Knight":
    print("apple 5")
elif selector == "The Detective Is Already Dead":
    print("blueberry 6")
elif selector == "Shimoneta":
    print("lemon 7")
elif selector == "I'm Quitting Heroing":
    print("cherry 8")
elif selector == "The Greateast Mage":
    print("orange 9")
else:
    print("Not Found!")

Output:

Project Select

Summary Of Project: This Project Has The Goal To Help Select What Anime I Should Watch Next

The Detective Is Already Dead
blueberry 6

The if-statement will give a true if the string is not ("") empty. For example:

if("helloworld"):
    print("yes")    #yes

if(""):
    print("yes")    #no output
Answered By: perpetualstudent

As an alternative to the answers offered, you may wish to use a dictionary to associate names with foods.

assoc = {
  'Black Bullet':                  'Banana 1',
  'Princess Connect':              'Watermelon 2',
  'Overlord':                      'Strawberry 3',
  'Date A Live':                   'kiwi 4',
  'Chivalry Of A Failed Knight':   'apple 5',
  'The Detective Is Already Dead': 'blueberry 6',
  'Shimoneta':                     'lemon 7',
  'I'm Quitting Heroing':         'cherry 8'
}

Now you can simply:

print(assoc.get(selector, 'Not found!'))

Using assoc.get(selector, 'Not found!') rather than assoc[selector] lets us supply a default value to return if the index is not found in the dictionary, thus replacing the else in a if/elif/else statement.

If you need to get all of the names, you can just use:

list(assoc.keys())
Answered By: Chris