简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM