2

Why does my python file run perfectly in the IDLE but doesn't work when I double click it. Let me rephrase, My if/else statements never seem to be true even tho they work correctly in the IDLE.

I even broke down ALL my code to the most simple if/else statement to test and make sure I wasn't missing something. Here is the code I broke down. this is the exact code in the py file, again it works in IDLE but not when I double click the py file

choice = input('letter: ')
if choice == 'a':
    print ('that is an a')
    input('press any key to exit...')
else:
    print('that letter is not an a')
    input('press any key to exit')

btw python v3.2 windows 7

5
  • What's the problem? Do you get a stack trace or weird behavior? Commented Apr 21, 2011 at 5:35
  • What is the program that opens when you double click? Is it a python 3.2 command line? Commented Apr 21, 2011 at 5:36
  • no trace, it just defaults to my else statement Commented Apr 21, 2011 at 5:37
  • When you added more print statements, what did you learn? Commented Apr 21, 2011 at 9:47
  • actually that wasn't my real program, my real program was a full interface that used classes and functions but my selection screen wasn't working so I had to grind it down to the core and found out it was my if/else statement that wasn't working..all in all i learned to add .strip() until i learn more of how to prevent this in a easier way Commented Apr 21, 2011 at 17:30

2 Answers 2

3

try adding

choice = choice.strip()

this works for me

choice = input('letter: ')
choice = choice.strip()
if choice == 'a':
    print ('that is an a')
    input('press any key to exit...')
else:
    print('that letter is not an a')
    input('press any key to exit')

otherwise your input gives you the letter together with a newline and the if fails

Sign up to request clarification or add additional context in comments.

2 Comments

That did indeed work, without being a hag can u give me a reason why that happened so I'll know for future reference!
because when you write in the shell 'a'+'enter' input gets both keystrokes and returns 'a\n'.
1

input() might be getting a string with a line ending.

Try adding the line print(repr(choice)) right after the input, to see exactly what you're working with.

2 Comments

i didnt get to check this as the advice above u seemed to works...BUT THANK YOU SOOO MUCH FOR ALL THE QUICK RESPONSES, i signed up just 3 minutes ago because I couldn't find help on this anywhere...This is now my official python lobby!
If strip() fixed it, I am correct, and you were getting a line ending.

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.