944,137 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 366909
  • Python RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Dec 22nd, 2008
0

Re: How to do Input in Python?

print 'What is you name?'
Raw_input()
Last edited by Flare; Dec 22nd, 2008 at 3:04 pm.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Flare is offline Offline
1 posts
since Dec 2008
Dec 23rd, 2008
0

Re: How to do Input in Python?

Or you can one-line it:
print raw_input('What is your name? ')
Reputation Points: 16
Solved Threads: 35
Junior Poster
mn_kthompson is offline Offline
148 posts
since Nov 2007
Dec 23rd, 2008
0

Re: How to do Input in Python?

Click to Expand / Collapse  Quote originally posted by Flare ...
print 'What is you name?'
Raw_input()
If you run it you will get error
NameError: name 'Raw_input' is not defined

That is because your version of raw_input() is Raw_input()
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
May 31st, 2009
0

Re: How to do Input in Python?

Click to Expand / Collapse  Quote originally posted by reezin14 ...
From what I understand you would use, input as to prompt the user to input a number and raw_input to prompt a string
No. You would use raw_input() for both normally, but you add int() if what you want is an int, or float() if you want a floating point number.
Python Syntax (Toggle Plain Text)
  1. s = raw_input( "Type a string: " ).rstrip( '\n' )
  2. n = int( raw_input( "Type an int: " ).rstrip( '\n' ) )
  3. 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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
mac-guyver is offline Offline
4 posts
since May 2009
May 31st, 2009
0

Re: How to do Input in Python?

Click to Expand / Collapse  Quote originally posted by tharippala ...
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
Well, strip() will take any whitespace characters off the beginning or end of the string. If you really only want to take the newline off the end, do this:
Python Syntax (Toggle Plain Text)
  1. print "Now " + test.rstrip( '\n' ) + " doesn't have a break in it."
Reputation Points: 10
Solved Threads: 1
Newbie Poster
mac-guyver is offline Offline
4 posts
since May 2009
May 31st, 2009
0

Re: How to do Input in Python?

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:
Python Syntax (Toggle Plain Text)
  1. from sys import stderr
  2. print >>stderr, "Type a string: ",
  3. s = raw_input().rstrip( '\n' )
  4. print >>stderr, "You typed:", s
Then you get something like this:
Python Syntax (Toggle Plain Text)
  1. Type a string: hello
  2. You typed: hello
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:
Python Syntax (Toggle Plain Text)
  1. from sys import stderr
  2. stderr.write( "Type a string: " )
  3. stderr.flush()
  4. s = raw_input().rstrip( '\n' )
  5. print >>stderr, "You typed: ", s

(Hey, emacs control characters work in this editor, kewl!)
Reputation Points: 10
Solved Threads: 1
Newbie Poster
mac-guyver is offline Offline
4 posts
since May 2009
May 31st, 2009
0

Re: How to do Input in Python?

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)
  1. for line in open( filename ):
  2. line = line.rstrip( '\n' )
  3. # blah blah...
Reputation Points: 10
Solved Threads: 1
Newbie Poster
mac-guyver is offline Offline
4 posts
since May 2009
Jun 23rd, 2009
1

Re: How to do Input in Python?

Click to Expand / Collapse  Quote originally posted by Rete ...
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:

python Syntax (Toggle Plain Text)
  1. test = raw_input ("Input word").strip()
  2. 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)
  1. test = raw_input("Input word")
  2. 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)
  1. test = raw_input("Input word").strip()
  2. print "Why %s there a line break all the time?" % test

Or to use the first example:

python Syntax (Toggle Plain Text)
  1. var = raw_input("Type something: ").strip()
  2. print "You typed %s" % var

Or, maybe you have multiple vars:

python Syntax (Toggle Plain Text)
  1. name = raw_input("Enter your name: ").strip()
  2. email = raw_input("Enter your email: ").strip()
  3. 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)
  1. import sys
  2. def prompt():
  3. response = sys.stdin.readline().strip()
  4. return response
  5.  
  6. fields = [ "Name: ", "Email: ", "Phone: " ]
  7.  
  8. answers = []
  9. for field in fields:
  10. print field,
  11. v = prompt()
  12. answers += [v]
  13.  
  14. print """Hello %s!
  15. Your Email is: %s
  16. 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.
Reputation Points: 15
Solved Threads: 5
Light Poster
Hummdis is offline Offline
48 posts
since Jun 2009
Oct 12th, 2009
-2
Re: How to do Input in Python?
how to save in the memory of the phone with rms + python
Reputation Points: 10
Solved Threads: 1
Newbie Poster
aksegaly is offline Offline
4 posts
since Oct 2009
Oct 12th, 2009
0
Re: How to do Input in Python?
Click to Expand / Collapse  Quote originally posted by aksegaly ...
how to save in the memory of the phone with rms + python
Vary bad manners to hijack this thread for such question.

Start your own thread and give more info!
Last edited by bumsfeld; Oct 12th, 2009 at 3:30 pm.
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Python Forum Timeline: Repetitive code problem
Next Thread in Python Forum Timeline: grab mouse and key input outside of python window





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC