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?