1

I have the following

if('y' == input("Enter y to continue")):
    # do something

But it doesn't work, no matter what I type?

3
  • 9
    Works fine here - what exactly do you mean by "doesn't work"? Commented Jan 8, 2012 at 16:53
  • I mean, it doesn't enter the conditional lines, even when I type 'y'. I'm running it from a Windows 7 command line. Commented Jan 8, 2012 at 16:57
  • what are you doing inside the conditional? paste the code. Commented Jan 8, 2012 at 17:04

5 Answers 5

3

This is a known bug on windows, you can identify it by doing print(repr(input("put y: "))

Try this

input("put y:").strip().lower() == "y"
Sign up to request clarification or add additional context in comments.

7 Comments

Yeah, windows turns it into 'y\r'. Thanks for clarifying!
I can't reproduce that either (even after adding the missing parenthesis). Could you link to the bug report? In which Python versions is it present?
Can't reproduce it either - i.e. it works at least fine under my Win7 x64 install. Now crossplatform terminal support has more than a few "rough edges" especially under windows, so maybe it's a bug in older Windows versions?
I run Windows 7 x64, all patched up, and yet I can reproduce it.
@x74x61: Which exact Python version?
|
3

I can't reproduce this here:

>>> if('y' == input("Enter y to continue: ")):
...    print("Yeah!")
...
Enter y to continue: y
Yeah!
>>>

But I would do it differently anyway:

answer = input("Enter y to continue: ")
if answer.lower().startswith("y"):
   print("Yeah!")

also handles Y, Yes!, yes, please... etc. correctly.

3 Comments

your own solution indeed does work, but I still can't figure out why mine doesn't.
Maybe you should copy the relevant contents of your Python shell and post them in your question. You must be doing something differently.
Tim, I think the problem is solved, but your solution is still better. I didn't do anything differently. See accepted answer.
2

Works fine for me on 3.2.1 on Windows 7 x64:

>>> if input("Enter y to continue") == 'y': print("ok")
Enter y to continuey
ok

("continuey" is correct, it's just "continue" + my "y" input.)

Comments

1

I've just come across this issue, this is how I solved it (Python 3.4.1):

i = input ('Enter y to continue: ')
validity = i == 'y'

if validity == True:
    #do something

By using the Boolean type to identify whether the input was equal to y you are avoiding the bug, it's extremely simple too.

Comments

0

use if input("Enter y to continue").lower().startswith('y'):.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.