Python if elif and else statement not working

Question:

I’m trying to write a short-ish script to entertain myself and potentially others. When I run it it tends to just skip over the if elif and else statements.

import random

adventure = ["fantasy" , "sci-fi" , "pirate" , "pre-history"]
setting = ["ocean", "forest" , "desert" , "castle"]
while True:

    adven = []
    setti = []
    random.shuffle(adventure)
    random.shuffle(setting)
    adven.append(adventure[0])
    setti.append(setting[0])
    print(adven)
    print(setti)
    accept = input("Is this acceptable? ")
    if accept == "y" :
        print("Great, let us get started!")
        break
    else :
        print("I am so sorry, lets try again!")
adve = []
sett = []
adve.append(adven)
sett.append(setti)

if adve == "fantasy" :
    if sett == "ocean" :
        print("1")
    elif sett == "forest" :
        print("2")
    elif sett == "desert" :
        print("3")
    elif sett == "castle" :
        print("4")
if adve == "sci-fi" :
    if sett == "ocean" :
        print("5")
    elif sett == "forest" :
        print("6")
    elif sett == "desert" :
        print("7")
    elif sett == "castle" :
        print("8")
if adve == "pirate" :
    if sett == "ocean" :
        print("9")
    elif sett == "forest" :
        print("10")
    elif sett == "desert" :
        print("11")
    elif sett == "castle" :
        print("12")
if adve == "pre-history" :
    if sett == "ocean" :
        print("13")
    elif sett == "forest" :
        print("14")
    elif sett == "desert" :
        print("15")
    elif sett == "castle" :
        print("16")

print(adve)
print(sett)

Would I need to keep it in the while True loop? I am not sure what to do because I want to make sure it works before I get any real details written into the script.

Asked By: somebodyithink

||

Answers:

and there is another thing that’s wrong is that you just didn’t put else statements after elif statements if you want to improve your coding basics. I recommend you to do your projects by watching projects like yours.

For example, you need to add this "else" statements like this:

if adve == "fantasy" :
    if sett == "ocean" :
        print("1")
    elif sett == "forest" :
        print("2")
    elif sett == "desert" :
        print("3")
    elif sett == "castle" :
        print("4")
    else :
        print("I am so sorry, you picked a wrong choice")
Answered By: ArchRanger

Try in. As in:

if "fantasy" in advve:

Will likely work better for you than == here.

Why? Because you’re testing if a string is in a list of strings.

Don’t forget to swap your operands around. Unlike == in cares which comes first.

Answered By: candied_orange

As I understand, you would like to have multiple values in adven and setti, that’s why it is a list?

So, firstly, you should take the definition of these values outside of loop scope:

adven = []
setti = []
while True:
    ...

You are appending list to list, so you getting list in list.

adve = []
adve.append(["random", "stuff"])
print(adve)
# [['random', 'stuff']]

Comparing a list to a string is always false because you compare different types of values.

print(adve == "random")
# False
print(["random"] == "random")
# False

To fix it you should:

# adve.append(adven)
adve = adven.copy()  # or simply `adve = adven`, or just use adven instead adve.
print(adve)
# ['random', 'stuff']

And:

# adve == "random"
print("random" in adve)
# True

Btw, you should use pop() function so you wouldn’t get duplicates:

# adven.append(adventure[0])
# setti.append(setting[0])
adven.append(adventure.pop())
setti.append(setting.pop())

If I misunderstood, and you don’t need to handle multiple values for adven and setti, then your code should looks like this:

import random

adventure = ["fantasy", "sci-fi", "pirate", "pre-history"]
setting = ["ocean", "forest", "desert", "castle"]
while True:
    random.shuffle(adventure)
    random.shuffle(setting)
    adven = adventure[0]
    setti = setting[0]
    print(adven)
    print(setti)
    accept = input("Is this acceptable? ")
    if accept == "y":
        print("Great, let us get started!")
        break
    else:
        print("I am so sorry, lets try again!")
adve = adven
sett = setti


multiplier = 0
if adve == "fantasy":
    multiplier = 1
elif adve == "sci-fi":
    multiplier = 2
elif adve == "pirate":
    multiplier = 3
elif adve == "pre-history":
    multiplier = 4
else:
    print(f"issue with adve: {adve}")

if sett == "ocean":
    print(multiplier * 1)
elif sett == "forest":
    print(multiplier * 2)
elif sett == "desert":
    print(multiplier * 3)
elif sett == "castle":
    print(multiplier * 4)

print(adve)
print(sett)
Answered By: ZabielskiGabriel
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.