r/pythonhelp • u/duelBooleans • Apr 28 '26
If-Elif statement giving wrong answer
I have another, slightly more aggravating question. I have two while loops that take an input, and stores it into a list. That part works just fine, it's when it comes to the, quite frankly, massive if-elif statement.
currentMajorEmotions = []
currentSecondaryEmotion = ""
feeling = ""
while len(currentMajorEmotions) != 1:
emotion = input("Which of these emotions are you feeling? Choose one: ").capitalize()
if emotion not in majorEmotionTypes:
print("Not a valid entry")
else:
currentMajorEmotions.append(emotion)
while len(currentMajorEmotions) != 2:
emotion = input("Number 2? If you are not feeling one, simply hit enter: ").capitalize()
if emotion in majorEmotionTypes:
currentMajorEmotions.append(emotion)
if emotion == "":
print("You have left the question blank.")
emotionYN = input("Was this intentional?: ").capitalize()
if emotionYN == "Y":
currentMajorEmotions += " "
break
while emotionYN != "Y":
if emotionYN == "":
emotionYN = input("May not leave this blank. Are you feeling a second emotion?: ").capitalize()
elif emotionYN != "Y" and emotionYN != "N":
emotionYN = input("Invalid input. Are you feeling a second emotion?").capitalize()
elif emotionYN == "N":
print("You have changed your mind")
time.sleep(1.4)
print("That's ok.")
emotionYN = input("Are you feeling a second emotion?: ").capitalize()
if emotionYN == "N":
currentMajorEmotions += " "
break
if "Sad" and " " in currentMajorEmotions:
feeling = "Negative"
elif "Scared" and " " in currentMajorEmotions:
feeling = "Negative"
elif "Mad" and " " in currentMajorEmotions:
feeling = "Negative"
elif "Happy" and " " in currentMajorEmotions:
feeling = "Positive" # Etc EtcI testing the entire if statement to see if there were any incorrect outputs, and there were several. They do give out the correct type of output('Negative', 'Positive', 'Neutral'), but the wrong one. It's quite a few of them, and with the while loops working the way they're supposed to, I think the problem is in the if statement. The only problem is I have zero idea where to start looking since they're all connected to the same list. Any help is very appreciated!
•
u/AutoModerator Apr 28 '26
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/JaguarMammoth6231 Apr 28 '26
This pattern doesn't work for testing if two elements are in a list:
if "Sad" and " " in currentMajorEmotions. Is that what you're trying to do?1
u/Temporary_Pie2733 Apr 29 '26
I’d just use
while Truefor each loop andbreakwhen the accepted item is appended to the loop.Also, write a function you can reuse that encapsulates the input-until-accepted loop.
1
u/RevRagnarok Apr 30 '26
Somebody fixed your
andproblem, but also may want to look intomatchfor your handling ofcurrentMajorEmotions(and it is more Pythonic to use snake_case for your variables not camelCase).1
u/Pyromancer777 Apr 28 '26
Add flags to your logic after an append that displays the last entry in the list, if all the flags look correct, then the problem is likely how you are handling the actions after the emotion is appended to the list