1,105,214 Community Members

How to do Input in Python?

Member Avatar
Cup of Squirrel
Junior Poster
131 posts since Oct 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
1
 

I've skimmed the manual and cant find the answer to this anywhere:

In basic, to add an Input function you would do (for example):

Input "What is your name? " name$

How do I do this in Python?

Member Avatar
alc6379
Cookie... That's it
2,752 posts since Dec 2003
Reputation Points: 105 [?]
Q&As Helped to Solve: 154 [?]
Skill Endorsements: 4 [?]
Team Colleague
 
0
 

According to my trusty Python Pocket Reference, it's the input() function:

input([Prompt])

Where [ Prompt ] is the string you would want to prompt the user with, obviously. So, from what I can tell:

foo=input('Please enter a value:')

I'm rusty, but I think that would print Please enter a value:, and assign the user input to the variable foo. Or, some combination of that should work. Like I said, I'm kind of rusty, but plan on jumping back in to Python Real Soon Now...

Member Avatar
DrLight
Newbie Poster
2 posts since Nov 2004
Reputation Points: 4 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
1
 

the correct way is

var = raw_input("Enter something: ")
print "you entered ", var

using input is unsafe as it evaluates your python code as commands and can cause someone to hack your code.

Member Avatar
alc6379
Cookie... That's it
2,752 posts since Dec 2003
Reputation Points: 105 [?]
Q&As Helped to Solve: 154 [?]
Skill Endorsements: 4 [?]
Team Colleague
 
0
 

the correct way is

var = raw_input("Enter something: ")
print "you entered ", var

using input is unsafe as it evaluates your python code as commands and can cause someone to hack your code.

I didn't know that! You've at least enlightened two people.

Just out of curiosity, what does raw_input() do different from input()? Does it escape out everything, like !'s, /'s, and such? Or, do something to where they couldn't define/change other variables with their input? Am I close?

Member Avatar
DrLight
Newbie Poster
2 posts since Nov 2004
Reputation Points: 4 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I didn't know that! You've at least enlightened two people.

Just out of curiosity, what does raw_input() do different from input()? Does it escape out everything, like !'s, /'s, and such? Or, do something to where they couldn't define/change other variables with their input? Am I close?

raw_input accepts your input as a string. input accepts it as a command. so for example lets say you had

input("somehthing")

and a hacker knowing some exploit in your code or system used the python function

eval

which basically evaluates a string as a command. you wouldn't want that. I had some code lying around here somewhere when i was testing it out, i'll try and find it for you.
For now just remeber to use only raw_input, if its imperitive to have a num then simply

int(input)

should do the trick.

Member Avatar
reezin14
Junior Poster
107 posts since Jan 2005
Reputation Points: 1 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

I've skimmed the manual and cant find the answer to this anywhere:

In basic, to add an Input function you would do (for example):

Input "What is your name? " name$

How do I do this in Python?

From what I understand you would use, input as to prompt the user to input a number and raw_input to prompt a string

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
1
 

The correct answer is from
http://www.daniweb.com/techtalkforums/thread20774.html

# raw_input() reads every input as a string
# then it's up to you to process the string
str1 = raw_input("Enter anything:")
print "raw_input =", str1

# input() actually uses raw_input() and then tries to
# convert the input data to a number using eval()
# hence you could enter a math expression
# gives an error if input is not numeric eg. $34.95
x = input("Enter a number:")
print "input =", x
Member Avatar
Rete
Newbie Poster
23 posts since Jun 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

Just a moment ...

I can't repeat your problem, both PythonWin and IDLE give a perfect one line result!

I tried to mimic your predicament ...

test = raw_input ("Input word")
print "Why "+ test +" there a line break all the time?"

# add a newline to the end for testing
test = test + '\n'
print "Why "+ test +" there a line break all the time?"

# simple way to remove a trailing newline
if '\n' in test:
    test = test[:-1]
print "Why "+ test +" there a line break all the time?"
Member Avatar
tharippala
Newbie Poster
2 posts since Oct 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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

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

Member Avatar
Flare
Newbie Poster
1 post since Dec 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

print 'What is you name?'
Raw_input()

Member Avatar
mn_kthompson
Junior Poster
148 posts since Nov 2007
Reputation Points: 3 [?]
Q&As Helped to Solve: 35 [?]
Skill Endorsements: 0 [?]
 
0
 

Or you can one-line it:
print raw_input('What is your name? ')

Member Avatar
Stefano Mtangoo
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 441 [?]
Q&As Helped to Solve: 394 [?]
Skill Endorsements: 2 [?]
 
0
 

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()

Member Avatar
mac-guyver
Newbie Poster
4 posts since May 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

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.

Member Avatar
mac-guyver
Newbie Poster
4 posts since May 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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:

print "Now " + test.rstrip( '\n' ) + " doesn't have a break in it."
Member Avatar
mac-guyver
Newbie Poster
4 posts since May 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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:

from sys import stderr
print >>stderr, "Type a string: ",
s = raw_input().rstrip( '\n' )
print >>stderr, "You typed:", s

Then you get something like this:

Type a string: hello
 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:

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!)

Member Avatar
mac-guyver
Newbie Poster
4 posts since May 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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.,

for line in open( filename ):
   line = line.rstrip( '\n' )
   # blah blah...
Member Avatar
Hummdis
Light Poster
48 posts since Jun 2009
Reputation Points: 5 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
1
 

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: joe@domain.com
Phone: 555-1234

Hi Joe!
Your Email is: joe@domain.com
Your Phone is: 555-1234

Hope that helps!

Member Avatar
aksegaly
Newbie Poster
4 posts since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
-2
 

how to save in the memory of the phone with rms + python

Member Avatar
bumsfeld
Posting Virtuoso
1,537 posts since Jul 2005
Reputation Points: 399 [?]
Q&As Helped to Solve: 261 [?]
Skill Endorsements: 7 [?]
 
0
 

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!

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article