1.11M Members

How to do Input in Python?

 
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?

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

 
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.

 
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?

 
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.

 
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

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

 
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?"
 
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

 
0
 

print 'What is you name?'
Raw_input()

 
0
 

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

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

 
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.

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

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

 
-2
 

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

 
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
View similar articles that have also been tagged: