Help with else statements or elif

Hello everyone, I am new to coding and I am doing the 100 days of coding course. There was a lesson where we ask the user “Do you like this?” if they answer “Yes” - “you are x” (ends here). If you answer “No” - “You’re not x.” , you get next question: “Do you have this” - same principle.

Now the way I did it is shown here:

print("""Marvel Movie Character Creation

""")
spajdermen = input("Do you like hanging out?  ")
if spajdermen == "Yes":
  print("Then you're Spiderman")
else:
  print("Then you're not Spiderman")
  korg = input("Do you have a gravelly voice? ")
  if korg == "Yes":
    print("Aww, then you're Korg")
  else:
    print("Aww, then you're not Korg")
    marvel = input("Do you often feel marvelous? ")
    if marvel == "Yes":
      print("Then you're Captain America")
    else:
      print("Honestly, I have no idea what you are")

I don’t know what happened to the indentations, so I’ll post a screenshot here

This program works.

The problem occured when I learned elif statements and tried making it “more elegant”. So instead of having 3 indented ifs, I would just have elifs. Now, the thing is that elif (to my limited knowledge) only checks the answer to some previous question it doesn’t let you ask a new question.
What I meant by that:

Are you spiderman?
yes: print you’re spiderman
otherwise print aww you’re not spiderman but do you like cats?
if yes print xy
otherwise: print well you’re not catwoman either, do you like green
ELSE print I don’t know what you are.

So it would only proceed with the next input if the answer to the previous one is negative. And it would only get to the else statement if none above are true, all others would be elifs.

I tried to do it like this, but of course, I can’t have a variable in row 5 and declare it in row 6 (elif korg == “Yes”)…

spajdermen = input("Do you like hanging out?  ")
if spajdermen == "Yes":
  print("Then you're Spiderman")
elif korg == "Yes":
  korg = input("""Then you're not Spiderman
  
  Do you have a gravelly voice? """)

Is this something that can be done with elif, or is the only way for this to work, the multiple if statements as shown in the screenshot?

Best regards :heartpulse:

You can refer to the guide for code formatting:

Basically, put three backticks (`) around your code:

print("Hello, world!")
Raw
```py
print("Hello, world!")
```

A simple solution would be not to use variables and just put the inputs in the if statements, although this will probably look less elegant if your inputs have long messages.

if input("...") == "Yes":
  # ...
elif input("...") == "Yes":
  # ...
else:
  # ...
3 Likes

I appreciate both advices.

Regarding the copying of code: you solved a problem I didn’t even know I had.

Regarding the variables and input: This is amazing. It pointed me towards a different way of thinking. I did it both your way and then I did it again by declaring the variables before the if statements.
(And to display my newly learnt skills of project sharing, I am pasting the link to my code https://replit.com/@HatidzaSerdare1/WEEK-5-DEBUG) :grin:

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.