Issue
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
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 if
- elif
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
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.