Isov5
Isov5

Reputation: 379

Why does my code work in python shell but not when I double click the py file

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

Upvotes: 2

Views: 1100

Answers (2)

joaquin
joaquin

Reputation: 85613

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

Upvotes: 3

rmmh
rmmh

Reputation: 7095

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.

Upvotes: 1

Related Questions