168k views
2 votes
What is the mistake in this python code

emotion = input("How are you feeling today?")
print(f"Im feeling {emotion} too!")

if == sad:
print("I hope you have a good day")

if == happy:
print("Me too")

User Jmkg
by
7.6k points

2 Answers

7 votes

The mistake in this Python code is that the if statements are missing the variable name that they should be checking. The correct syntax for an if statement is `if condition:` where `condition` is some expression that evaluates to either True or False.

Also, the values `sad` and `happy` are not defined as strings in the code, so the comparison with these values will result in a NameError.

Here's the corrected code:

emotion = input("How are you feeling today?")

print(f"I'm feeling {emotion} too!")

if emotion == "sad":

print("I hope you have a good day.")

if emotion == "happy":

print("Me too.")

Note that I've added the variable name `emotion` to the if statements, and defined the comparison values as strings. Also, I've added indentation to the print statements so they are executed only when the condition is true.

User Aalhanane
by
6.5k points
0 votes

You will need to set the if statements to:

if emotion == "sad":

print("I hope you have a good day")

if emotion == "happy":

print("Me too")

Make sure to indent the print code, and the first part of your code is correct!

User Adam Right
by
6.8k points