943,755 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 366218
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 12th, 2004
1

How to do Input in Python?

Expand Post »
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)
  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
Reputation Points: 11
Solved Threads: 1
Junior Poster
Cup of Squirrel is offline Offline
133 posts
since Oct 2004
Oct 12th, 2004
0

Re: How to do Input in Python?

According to my trusty Python Pocket Reference, it's the input() function:

Python Syntax (Toggle Plain Text)
  1. 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)
  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...
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Nov 12th, 2004
1

Re: How to do Input in Python?

the correct way is
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 14
Solved Threads: 1
Newbie Poster
DrLight is offline Offline
2 posts
since Nov 2004
Dec 2nd, 2004
0

Re: How to do Input in Python?

Quote originally posted by DrLight ...
the correct way is
Python Syntax (Toggle Plain Text)
  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?
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Dec 2nd, 2004
0

Re: How to do Input in Python?

Quote 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)
  1. input("somehthing")
and a hacker knowing some exploit in your code or system used the python function
Python Syntax (Toggle Plain Text)
  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
Python Syntax (Toggle Plain Text)
  1. int(input)
should do the trick.
Reputation Points: 14
Solved Threads: 1
Newbie Poster
DrLight is offline Offline
2 posts
since Nov 2004
Jan 19th, 2005
0

Re: How to do Input in Python?

Quote 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)
  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
Reputation Points: 12
Solved Threads: 4
Junior Poster
reezin14 is offline Offline
107 posts
since Jan 2005
Mar 30th, 2005
1

Re: How to do Input in Python?

The correct answer is from
http://www.daniweb.com/techtalkforums/thread20774.html
Python Syntax (Toggle Plain Text)
  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
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jun 13th, 2005
0

Re: How to do Input in Python?

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Rete is offline Offline
23 posts
since Jun 2005
Jun 13th, 2005
0

Re: How to do Input in Python?

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 ...
Python Syntax (Toggle Plain Text)
  1. test = raw_input ("Input word")
  2. print "Why "+ test +" there a line break all the time?"
  3.  
  4. # add a newline to the end for testing
  5. test = test + '\n'
  6. print "Why "+ test +" there a line break all the time?"
  7.  
  8. # simple way to remove a trailing newline
  9. if '\n' in test:
  10. test = test[:-1]
  11. print "Why "+ test +" there a line break all the time?"
Last edited by vegaseat; Apr 26th, 2010 at 8:21 am. Reason: changed code tags
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 22nd, 2007
0

Re: How to do Input in Python?

Click to Expand / Collapse  Quote originally posted by Rete ...
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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
tharippala is offline Offline
2 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Python Forum Timeline: Repetitive code problem
Next Thread in Python Forum Timeline: grab mouse and key input outside of python window





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC