My program works fine in IDLE and PyScripter, but not in PowerShell and command line

落花浮王杯 提交于 2019-12-11 02:54:43

问题


This seems like a really basic question, but I wasn't able to find the solution anywhere, so here it goes:

I'm writing a small program, mostly for practice, that does difefrent things, depending on my input, like so:

while True:
    switch = input('a, b or c:')

    if switch == "a":
        print("In command line and Powershell...")
    elif switch == "b":
        print("...these lines will never run.")
    elif switch == "c":
        print("Neither will this one, no matter what my input(switch) is.")
    else:
         print("meh...")
                 break

If I run my code in IDLE or PyScripter interpreter it works fine, but when I run it in the command line or in PowerShell, no matter what my input is, the "else" line gets executed every time.


回答1:


I don't know why you get a carriage return at the end, are you running this on Windows, perhaps. On Unix it works well. The workaround is to make sure to strip any whitespace:

switch = input('a, b or c:').strip()

That should solve your problem.



来源:https://stackoverflow.com/questions/11769364/my-program-works-fine-in-idle-and-pyscripter-but-not-in-powershell-and-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!