How to do Input in Python?

Thread Solved

Join Date: Oct 2004
Posts: 133
Reputation: Cup of Squirrel is an unknown quantity at this point 
Solved Threads: 1
Cup of Squirrel's Avatar
Cup of Squirrel Cup of Squirrel is offline Offline
Junior Poster

How to do Input in Python?

 
0
  #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):

  1. 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
Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: How to do Input in Python?

 
0
  #2
Oct 12th, 2004
According to my trusty Python Pocket Reference, it's the input() function:

  1. input([Prompt])


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

  1. 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
Quick reply to this message  
Join Date: Nov 2004
Posts: 2
Reputation: DrLight is an unknown quantity at this point 
Solved Threads: 1
DrLight DrLight is offline Offline
Newbie Poster

Re: How to do Input in Python?

 
1
  #3
Nov 12th, 2004
the correct way is
  1. var = raw_input("Enter something: ")
  2. print "you entered ", var

using input is unsafe as it evaluates your python code as commands and can cause someone to hack your code.
Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: How to do Input in Python?

 
0
  #4
Dec 2nd, 2004
Originally Posted by DrLight
the correct way is
  1. var = raw_input("Enter something: ")
  2. 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
Quick reply to this message  
Join Date: Nov 2004
Posts: 2
Reputation: DrLight is an unknown quantity at this point 
Solved Threads: 1
DrLight DrLight is offline Offline
Newbie Poster

Re: How to do Input in Python?

 
0
  #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
  1. input("somehthing")
and a hacker knowing some exploit in your code or system used the python function
  1. 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
  1. int(input)
should do the trick.
Quick reply to this message  
Join Date: Jan 2005
Posts: 107
Reputation: reezin14 is an unknown quantity at this point 
Solved Threads: 4
reezin14's Avatar
reezin14 reezin14 is offline Offline
Junior Poster

Re: How to do Input in Python?

 
0
  #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):

  1. 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
Quick reply to this message  
Join Date: Oct 2004
Posts: 3,941
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 911
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: How to do Input in Python?

 
0
  #7
Mar 30th, 2005
The correct answer is from
http://www.daniweb.com/techtalkforums/thread20774.html
  1. # raw_input() reads every input as a string
  2. # then it's up to you to process the string
  3. str1 = raw_input("Enter anything:")
  4. print "raw_input =", str1
  5.  
  6. # input() actually uses raw_input() and then tries to
  7. # convert the input data to a number using eval()
  8. # hence you could enter a math expression
  9. # gives an error if input is not numeric eg. $34.95
  10. x = input("Enter a number:")
  11. print "input =", x
May 'the Google' be with you!
Quick reply to this message  
Join Date: Jun 2005
Posts: 23
Reputation: Rete is an unknown quantity at this point 
Solved Threads: 1
Rete Rete is offline Offline
Newbie Poster

Re: How to do Input in Python?

 
0
  #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.
Quick reply to this message  
Join Date: Oct 2004
Posts: 3,941
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 911
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: How to do Input in Python?

 
0
  #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!
Quick reply to this message  
Join Date: Oct 2007
Posts: 2
Reputation: tharippala is an unknown quantity at this point 
Solved Threads: 1
tharippala tharippala is offline Offline
Newbie Poster

Re: How to do Input in Python?

 
0
  #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
Quick reply to this message  
Closed Thread

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC