CyManSAP CyManSAP - 4 months ago 22
Python Question

Input problems in cmd

I'm having a problem where if I run my python program in the windows terminal, text with inserted variables (

%s
) have wacky results, where as in the python shell it works fine.

Code:

print("Hi! What's your name?")
name = input("name: ")
print("Nice to meet you %s" % name)
print("%s is a good name." % name)
print("This line is only to test %s in the middle of the text." % name)
input("press enter to exit")


Result in python shell:

Python Shell Result

Result in cmd:

cmd result

I'm using Windows 10 and python32 in case you needed to know.

Answer

This is a bug in the original 3.2.0 on Windows. The input() statement stripped off the "\n" but not the '\r', so the string input is hidden.

See https://bugs.python.org/issue11272

Quick fix:

name = input("name: ").rstrip()

It was fixed in 3.2.1. You really should upgrade your Python!