I have another problem with pythons Input, and I was wondering if someone could help me. Whenever I get any input from the user, python keeps printing out on a new line.
So for example:
test = raw_input ("Input word") print "Why "+ test +" there a line break all the time?"
So if my inputted word was "is", the program would output this:
Why is
there a line break all the time?
Im guessing its because python is including the ENTER key used whenever I'm finished with input, but I'm not sure how to stop that. Any help would be appreciated. :D
There's a faster fix to this, just use this:
test = raw_input ("Input word").strip()
print "Why "+ test +" there a line break all the time?"
That beats having to make sure you enter the ".strip()" every time that you want to print the var like this:
test = raw_input("Input word")
print "Why" + test.strip() + " there a line break all the time?"
You could also make it easier by doing this:
test = raw_input("Input word").strip()
print "Why %s there a line break all the time?" % test
Or to use the first example:
var = raw_input("Type something: ").strip()
print "You typed %s" % var
Or, maybe you have multiple vars:
name = raw_input("Enter your name: ").strip()
email = raw_input("Enter your email: ").strip()
print "Hello %s!\nThe email address we have for you is '%s'." % (name, email)
Or, better yet, make the prompt a function if you're asking for a lot of information:
import sys
def prompt():
response = sys.stdin.readline().strip()
return response
fields = [ "Name: ", "Email: ", "Phone: " ]
answers = []
for field in fields:
print field,
v = prompt()
answers += [v]
print """Hello %s!
Your Email is: %s
Your Phone is: %s""" % ( answers[0], answers[1], answers[2] )
With the above process, the user will see:Name: Joe
Email: [email]joe@domain.com[/email]
Phone: 555-1234
Hi Joe!
Your Email is: [email]joe@domain.com[/email]
Your Phone is: 555-1234
Hope that helps!