My program works fine in IDLE and PyScripter, but

2019-07-10 02:56发布

问题:

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.