| | |
How to do Input in Python?
Thread Solved |
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):
How do I do this in Python?
In basic, to add an Input function you would do (for example):
Python Syntax (Toggle Plain Text)
Input "What is your name? " name$
How do I do this in Python?
Last edited by Cup of Squirrel; Oct 12th, 2004 at 2:22 pm. Reason: Whoops. I put Perl instead of Python in title
According to my trusty Python Pocket Reference, it's the input() function:
Where [ Prompt ] is the string you would want to prompt the user with, obviously. So, from what I can tell:
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...
Python Syntax (Toggle Plain Text)
input([Prompt])
Where [ Prompt ] is the string you would want to prompt the user with, obviously. So, from what I can tell:
Python Syntax (Toggle Plain Text)
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...
Alex Cavnar, aka alc6379
•
•
Join Date: Nov 2004
Posts: 2
Reputation:
Solved Threads: 1
the correct way is
using input is unsafe as it evaluates your python code as commands and can cause someone to hack your code.
Python Syntax (Toggle Plain Text)
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.
•
•
•
•
Originally Posted by DrLight
the correct way isPython Syntax (Toggle Plain Text)
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.
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?
Alex Cavnar, aka alc6379
•
•
Join Date: Nov 2004
Posts: 2
Reputation:
Solved Threads: 1
•
•
•
•
Originally Posted by alc6379
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
Python Syntax (Toggle Plain Text)
input("somehthing")
Python Syntax (Toggle Plain Text)
eval
For now just remeber to use only raw_input, if its imperitive to have a num then simply
Python Syntax (Toggle Plain Text)
int(input)
•
•
•
•
Originally Posted by Cup of Squirrel
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):
Python Syntax (Toggle Plain Text)
Input "What is your name? " name$
How do I do this in Python?
The correct answer is from
http://www.daniweb.com/techtalkforums/thread20774.html
http://www.daniweb.com/techtalkforums/thread20774.html
Python Syntax (Toggle Plain Text)
# 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
May 'the Google' be with you!
•
•
Join Date: Jun 2005
Posts: 23
Reputation:
Solved Threads: 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.
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.
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 ...
[php]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?"
[/php]
I can't repeat your problem, both PythonWin and IDLE give a perfect one line result!
I tried to mimic your predicament ...
[php]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?"
[/php]
May 'the Google' be with you!
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Solved Threads: 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.
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
![]() |
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 |
alarm ansi app assignment avogadro backend beginner binary bluetooth character cipher cmd customdialog cx-freeze data decimals dictionary directory dynamic error exe file float format function generator getvalue gnu graphics halp heads homework http ideas import input ip itunes java keycontrol leftmouse line linux list lists loop maintain maze millimeter module mouse number numbers output parsing path pointer prime programming progressbar push py2exe pygame python queue random recursion schedule screensaverloopinactive script slicenotation sqlite ssh statistics string strings sudokusolver sum text thread threading time tlapse tuple tutorial ubuntu unicode url urllib urllib2 variable variables ventrilo vigenere web webservice wikipedia write wxpython xlib





