how to enter input in python

Question:

I am taking part in a competition, but have no idea how to make python take the input. Here’s a typical example . The site has given example of how to take input but those are using C and Java ( click here ). Please help me figure out how to make python take the input in this case.

Asked By: Lokesh

||

Answers:

You can use either input or raw_input functions.

Note: input is prone to security issues, so use raw_input instead.

For example:

inputString = raw_input()

this will read the input line and store it in inputString. If you want an int,

inputInt = int(raw_input())
Answered By: thefourtheye

The best way to take input for a user is using raw_input, this will take in user input as a string. Let me demonstrate:

>>> var = raw_input("Enter")
Enter>? happy
>>> var
'happy'

Notice the quote-marks on happy, this indicates a string. You may also notice, input, and yes that can be used to take user input, but here is one example where that is a bad idea:

>>> a = 2
>>> input("Enter")
Enter>? a+1
3

Here, input actually gets evaluated, since we’ve already declared a, a + 1 == 3, and we see that as the output in our console session. This later becomes a security concern (you would not want users messing around with your variables), so for user input, raw_input is the best choice.

Since you get a string from raw_input, you can convert it to whatever you like, if it can be converted, for example:

>>> var = raw_input("Enter")
Enter>? 122
>>> var = int(var)
>>> var
122

However, floats will not work with int:

>>> int('1.223')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1.223'

You will need to use float here, then it works:

>>> float('1.223')
1.223
Answered By: Games Brainiac

There’s more than one way to do this.

Taking input from the intepreter

For Python 2.x, you can use the raw_input() function:

my_input = raw_input("Please enter an input: ")
#do something with my_input

Note that the input is always a string. To retrieve a number, you can use the built-in int() function:

my_input = int(raw_input("Please enter an input: "))
#do something with my_input

As one other answer mentioned, this will throw an error if the input is a float.

There’s also another function, input, in Python 2.x. However, in this version of Python, input evaluates the input, which is a bad idea. It’s not recommended to use it.

For Python 3.x, however, you can use the input() function without any problem, since it’s a replacement for raw_input:

my_input = input("Please enter an input: ")
#do something with my_input

Taking input from command line arguments

You can also retrieve your input from command line arguments, when executing your script like this:

$ python my_script.py arg1 arg2

The arguments will be stored in the list sys.argv. sys.argv[0] is the first argument, sys.argv[1] is the second argument, and so on.
Example:

import sys
my_input = sys.argv[0]
#do something with my_input

See the details of it here
This method works for both versions, Python 3.x and 2.x .

Hope this helps!

Answered By: aIKid
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.