繁体   English   中英

为什么我的代码可以在python shell中工作,但是当我双击py文件时却不能工作

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

为什么我的python文件在IDLE中可以正常运行,但是当我双击它时却不起作用。 让我重新表述一下,即使它们在IDLE中正常运行,我的if / else语句也似乎永远都不成立。

我什至将所有代码分解为最简单的if / else语句进行测试,并确保我没有丢失任何内容。 这是我分解的代码。 这是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

尝试添加

choice = choice.strip()

这对我有用

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')

否则,您的input将给您提供字母和换行符, if失败

input()可能正在获取一个以行结尾的字符串。

尝试在输入之后立即添加print(repr(choice)) ,以查看您正在使用的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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