简体   繁体   English

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

[英]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.

相关问题 为什么我的代码在IDLE中工作,但在保存文件并双击文件后却无法工作 - Why does my code work in IDLE but not when I save the file and double click the file 为什么我的代码可以在IDLE中工作,但在保存文件并双击文件后却不能工作? - why does my code works in IDLE but not when I save the file and double click the file? 为什么我的Selenium代码在Python Shell中可以工作,但不能在文件中工作? - Why does my Selenium code work in the Python Shell but not from a file? 为什么我的代码在运行 in.py 文件时可以工作,但在 Python 解释器中返回 SyntaxError? - Why does my code work when run in .py file, but returns a SyntaxError in Python interpreter? 为什么我的python程序双击.sh文件不运行 - Why does my python program not run when I double click the .sh file 为什么我的 TKinter GUI 代码可以在交互式 shell 中工作,但从文件中运行时却不能? - Why does my TKinter GUI code work from interactive shell, but not when run from a file? 为什么我的代码在交互式Shell中起作用,而在从文件运行时却不能起作用? - Why does my code work from interactive shell, but not when run from a file? 为什么此python代码将文件大小加倍而不更改? - Why does this python code double the size of my file without changing it? 双击时,Python Tkinter - .py文件不起作用 - Python Tkinter - .py file doesn't work when double clicked 为什么我在telnet上运行时,为什么此代码无法在python中工作? - Why won't this code work in python but when I telnet It does?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM