Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading