[英]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. 为什么我的python文件在IDLE中可以正常运行,但是当我双击它时却不起作用。 Let me rephrase, My if/else statements never seem to be true even tho they work correctly in the IDLE.
让我重新表述一下,即使它们在IDLE中正常运行,我的if / else语句也似乎永远都不成立。
I even broke down ALL my code to the most simple if/else statement to test and make sure I wasn't missing something. 我什至将所有代码分解为最简单的if / else语句进行测试,并确保我没有丢失任何内容。 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
这是py文件中的确切代码,再次在IDLE中有效,但是当我双击py文件时不起作用
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 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
将给您提供字母和换行符, if
失败
input() might be getting a string with a line ending. input()可能正在获取一个以行结尾的字符串。
Try adding the line print(repr(choice))
right after the input, to see exactly what you're working with. 尝试在输入之后立即添加
print(repr(choice))
,以查看您正在使用的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.