| | |
How to do Input in Python?
Thread Solved |
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
•
•
Join Date: May 2009
Posts: 4
Reputation:
Solved Threads: 1
•
•
•
•
From what I understand you would use, input as to prompt the user to input a number and raw_input to prompt a string
Python Syntax (Toggle Plain Text)
s = raw_input( "Type a string: " ).rstrip( '\n' ) n = int( raw_input( "Type an int: " ).rstrip( '\n' ) ) f = float( raw_input( "Type a float: " ).rstrip( '\n' ) )
The "rstrip('\n')" takes the newline character off the end of what they type.
The only time you would use input() is if you want the user to be able to type any Python code they want, so they have the power to type in a complicated expression like "sin( pi/7 )" ... or erase all your files ... as part of their answer.
•
•
Join Date: May 2009
Posts: 4
Reputation:
Solved Threads: 1
•
•
•
•
Looks like a funny issue, I couldn't re - produce it. But Ican suggest you the solution.
try this,
print "Why "+ test.strip() +" there a line break all the time?"
It has to work out, strip will help you to remove the newline charecter from test
Python Syntax (Toggle Plain Text)
print "Now " + test.rstrip( '\n' ) + " doesn't have a break in it."
•
•
Join Date: May 2009
Posts: 4
Reputation:
Solved Threads: 1
stdout vs. stderr
One more issue with input() and raw_input() is that they print the prompt on the standard output! In Unix I almost always want prompts to go to standard error instead. But doing this is tricky. If you do this:
Then you get something like this:
An extra space comes out on the next print to stderr because of the comma when you printed the prompt. The only correct way to do it I know is this:
(Hey, emacs control characters work in this editor, kewl!)
One more issue with input() and raw_input() is that they print the prompt on the standard output! In Unix I almost always want prompts to go to standard error instead. But doing this is tricky. If you do this:
Python Syntax (Toggle Plain Text)
from sys import stderr print >>stderr, "Type a string: ", s = raw_input().rstrip( '\n' ) print >>stderr, "You typed:", s
Python Syntax (Toggle Plain Text)
Type a string: hello You typed: hello
Python Syntax (Toggle Plain Text)
from sys import stderr stderr.write( "Type a string: " ) stderr.flush() s = raw_input().rstrip( '\n' ) print >>stderr, "You typed: ", s
(Hey, emacs control characters work in this editor, kewl!)
•
•
Join Date: May 2009
Posts: 4
Reputation:
Solved Threads: 1
Forgot, I wanted to say: rstrip( '\n' ) is what you typically want to do to lines you read from a file. It won't take indentation off the lines! E.g.,
Python Syntax (Toggle Plain Text)
for line in open( filename ): line = line.rstrip( '\n' ) # blah blah...
•
•
•
•
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
python Syntax (Toggle Plain Text)
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:
python Syntax (Toggle Plain Text)
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:
python Syntax (Toggle Plain Text)
test = raw_input("Input word").strip() print "Why %s there a line break all the time?" % test
Or to use the first example:
python Syntax (Toggle Plain Text)
var = raw_input("Type something: ").strip() print "You typed %s" % var
Or, maybe you have multiple vars:
python Syntax (Toggle Plain Text)
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:
python Syntax (Toggle Plain Text)
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: joe@domain.com
Phone: 555-1234
Hi Joe!
Your Email is: joe@domain.com
Your Phone is: 555-1234
Hope that helps!
Last edited by Hummdis; Jun 23rd, 2009 at 3:54 pm. Reason: Gramatical changes.
![]() |
Other Threads in the Python Forum
- Previous Thread: Repetitive code problem
- Next Thread: grab mouse and key input outside of python window
| Thread Tools | Search this Thread |
abrupt ansi anti apache approximation array assignment avogadro backend beginner binary book builtin calculator character code converter countpasswordentry curved customdialog dan08 dictionaries dictionary dynamic examples exe file float format function gnu graphics gui heads homework import inches input java launcher library line lines linux list lists loop mouse mysql mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame pysimplewizard python random recursion redirect scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable wordgame write wxpython xlib





