Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't this if statement work? New to python 3 [closed]

I can't figure out for the life of me why this if statement isn't working in Python 3. I have always worked with python 2.7 but I need to get familiar with 3. Here is my code

print("Answer the question! [(Y)es or (N)o]: ")
answer = input()
print(answer)

if answer == "y":
    print("OK")

print("done")

I start this code, get presented with the question, ok, that's normal. For input I give it a single lowercase y. I see that 'y' printed back to me, but then the program bypasses the if statement and goes straight to done. What simple thing am I doing wrong?

like image 253
Michael Fryer Avatar asked Nov 04 '22 09:11

Michael Fryer


2 Answers

Well for starters, your code works! I've tested it online and it works. Probably something with your IDE or with whatever Python you are using. I've had errors like this when using Jython.

But it works here!

like image 172
turnt Avatar answered Nov 12 '22 21:11

turnt


I dont see any problem http://ideone.com/Vk9Hdo, Try this:

print("Answer the question! [(Y)es or (N)o]: ")
answer = input()
print(answer)

if answer == "y":
    print("OK")

print("done")

Output

Answer the question! [(Y)es or (N)o]: 
y
OK
done
like image 25
Bobb Dizzles Avatar answered Nov 12 '22 19:11

Bobb Dizzles