User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Oct 2004
Location: Southwest UK
Posts: 131
Reputation: Cup of Squirrel is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Cup of Squirrel's Avatar
Cup of Squirrel Cup of Squirrel is offline Offline
Junior Poster

How to do Input in Python?

  #1  
Oct 12th, 2004
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?
Last edited by Cup of Squirrel : Oct 12th, 2004 at 1:22 pm. Reason: Whoops. I put Perl instead of Python in title
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 11
Solved Threads: 101
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: How to do Input in Python?

  #2  
Oct 12th, 2004
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...
Alex Cavnar, aka alc6379
Reply With Quote  
Join Date: Nov 2004
Posts: 2
Reputation: DrLight is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
DrLight DrLight is offline Offline
Newbie Poster

Re: How to do Input in Python?

  #3  
Nov 12th, 2004
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.
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 11
Solved Threads: 101
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: How to do Input in Python?

  #4  
Dec 2nd, 2004
Originally Posted by DrLight
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?
Alex Cavnar, aka alc6379
Reply With Quote  
Join Date: Nov 2004
Posts: 2
Reputation: DrLight is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
DrLight DrLight is offline Offline
Newbie Poster

Re: How to do Input in Python?

  #5  
Dec 2nd, 2004
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")
and a hacker knowing some exploit in your code or system used the python function 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.
Reply With Quote  
Join Date: Jan 2005
Posts: 107
Reputation: reezin14 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
reezin14's Avatar
reezin14 reezin14 is offline Offline
Junior Poster

Re: How to do Input in Python?

  #6  
Jan 19th, 2005
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
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,394
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 172
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Solution Re: How to do Input in Python?

  #7  
Mar 30th, 2005
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
May 'the Google' be with you!
Reply With Quote  
Join Date: Jun 2005
Posts: 23
Reputation: Rete is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Rete Rete is offline Offline
Newbie Poster

Re: How to do Input in Python?

  #8  
Jun 13th, 2005
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.
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,394
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 172
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Solution Re: How to do Input in Python?

  #9  
Jun 13th, 2005
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]
May 'the Google' be with you!
Reply With Quote  
Join Date: Oct 2007
Posts: 2
Reputation: tharippala is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tharippala tharippala is offline Offline
Newbie Poster

Re: How to do Input in Python?

  #10  
Oct 22nd, 2007
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.



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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Python Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 10:51 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC