•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 391,582 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,683 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Views: 83313 | Replies: 9
![]() |
•
•
Join Date: Oct 2004
Location: Southwest UK
Posts: 131
Reputation:
Rep Power: 0
Solved Threads: 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):
How do I do this in Python?
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?
Last edited by Cup of Squirrel : Oct 12th, 2004 at 1:22 pm. Reason: Whoops. I put Perl instead of Python in title
•
•
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation:
Rep Power: 11
Solved Threads: 101
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...
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...
Alex Cavnar, aka alc6379
•
•
Join Date: Nov 2004
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
the correct way is
using input is unsafe as it evaluates your python code as commands and can cause someone to hack your code.
var = raw_input("Enter something: ")
print "you entered ", varusing input is unsafe as it evaluates your python code as commands and can cause someone to hack your code.
•
•
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation:
Rep Power: 11
Solved Threads: 101
•
•
•
•
Originally Posted by DrLight
the correct way isvar = 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?
Alex Cavnar, aka alc6379
•
•
Join Date: Nov 2004
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
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
input("somehthing")For now just remeber to use only raw_input, if its imperitive to have a num then simply
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):
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
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,394
Reputation:
Rep Power: 9
Solved Threads: 172
The correct answer is from
http://www.daniweb.com/techtalkforums/thread20774.html
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 May 'the Google' be with you!
•
•
Join Date: Jun 2005
Posts: 23
Reputation:
Rep Power: 4
Solved Threads: 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.
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.
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,394
Reputation:
Rep Power: 9
Solved Threads: 172
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:
Rep Power: 0
Solved Threads: 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.
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
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
- Starting Python (Python)
Other Threads in the Python Forum
- Previous Thread: plotting 10 graphs in 1 figure
- Next Thread: newbee: list.append().append()



Linear Mode