How to do Input in Python?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2008
Posts: 1
Reputation: Flare is an unknown quantity at this point 
Solved Threads: 1
Flare Flare is offline Offline
Newbie Poster

Re: How to do Input in Python?

 
0
  #11
Dec 22nd, 2008
print 'What is you name?'
Raw_input()
Last edited by Flare; Dec 22nd, 2008 at 3:04 pm.
Quick reply to this message  
Join Date: Nov 2007
Posts: 148
Reputation: mn_kthompson is an unknown quantity at this point 
Solved Threads: 32
mn_kthompson mn_kthompson is offline Offline
Junior Poster

Re: How to do Input in Python?

 
0
  #12
Dec 23rd, 2008
Or you can one-line it:
print raw_input('What is your name? ')
Quick reply to this message  
Join Date: Jun 2007
Posts: 1,395
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 127
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: How to do Input in Python?

 
0
  #13
Dec 23rd, 2008
Originally Posted by Flare View Post
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()
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
Quick reply to this message  
Join Date: May 2009
Posts: 4
Reputation: mac-guyver is an unknown quantity at this point 
Solved Threads: 1
mac-guyver mac-guyver is offline Offline
Newbie Poster

Re: How to do Input in Python?

 
0
  #14
May 31st, 2009
Originally Posted by reezin14 View Post
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.
  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.
Quick reply to this message  
Join Date: May 2009
Posts: 4
Reputation: mac-guyver is an unknown quantity at this point 
Solved Threads: 1
mac-guyver mac-guyver is offline Offline
Newbie Poster

Re: How to do Input in Python?

 
0
  #15
May 31st, 2009
Originally Posted by tharippala View Post
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:
  1. print "Now " + test.rstrip( '\n' ) + " doesn't have a break in it."
Quick reply to this message  
Join Date: May 2009
Posts: 4
Reputation: mac-guyver is an unknown quantity at this point 
Solved Threads: 1
mac-guyver mac-guyver is offline Offline
Newbie Poster

Re: How to do Input in Python?

 
0
  #16
May 31st, 2009
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:
  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:
  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:
  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!)
Quick reply to this message  
Join Date: May 2009
Posts: 4
Reputation: mac-guyver is an unknown quantity at this point 
Solved Threads: 1
mac-guyver mac-guyver is offline Offline
Newbie Poster

Re: How to do Input in Python?

 
0
  #17
May 31st, 2009
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.,
  1. for line in open( filename ):
  2. line = line.rstrip( '\n' )
  3. # blah blah...
Quick reply to this message  
Join Date: Jun 2009
Posts: 11
Reputation: Hummdis is an unknown quantity at this point 
Solved Threads: 1
Hummdis's Avatar
Hummdis Hummdis is offline Offline
Newbie Poster

Re: How to do Input in Python?

 
1
  #18
Jun 23rd, 2009
Originally Posted by Rete View Post
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:

  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:

  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:

  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:

  1. var = raw_input("Type something: ").strip()
  2. print "You typed %s" % var

Or, maybe you have multiple vars:

  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:

  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.
Quick reply to this message  
Join Date: Oct 2009
Posts: 4
Reputation: aksegaly is an unknown quantity at this point 
Solved Threads: 1
aksegaly aksegaly is offline Offline
Newbie Poster
 
-2
  #19
Oct 12th, 2009
how to save in the memory of the phone with rms + python
Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso
 
0
  #20
Oct 12th, 2009
Originally Posted by aksegaly View Post
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.
Should you find Irony, you can keep her!
Quick reply to this message  
Closed Thread

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC