In Python programs, you may need to take user input from the users for performing certain actions. For example, asking a user to enter their names, age, Zip code, etc.
The input method takes the input (generally what the user types), and returns it back as a string.
Syntax:
The prompt is an optional parameter. If this is given, it is written to the standard output without a trailing newline.
I will show you examples of using the Python input function with leap years, square roots, and others in the later part of this tutorial, first, let me explain the difference between the Python raw_input and input functions.
Difference between input and raw_input
- The raw_input function was built in Python 2. It is deprecated in version 3.
- The raw_input worked the same way as input() function in Python 3 works. Alternatively, you can say the raw_input is renamed to input function Python version 3.
- In Python 2, the input() function was used to first take the raw_input() and then performing an eval() in it i.e. eval(raw_input(prompt)) was equivalent to input(prompt).
- In Python 3, the purpose of input() in Python 2 is removed and the same result can be achieved by using the eval(input()).
Now, let us see a few examples of using the input Python function.
An example of checking the leap year using Python input function
As you run the following program, the user will be asked to enter a year.
After entering the year and pressing the enter key, the entered value of the year will be calculated to check whether it is a leap year or not and the result will be displayed on the screen. Have a look:
#A Demo of telling leap year with input function try: a_Year = float(input("Enter a Year? ")) except ValueError: print ("Must enter digits!") else: Leap_Year = a_Year%4 == 0 if Leap_Year: print ("This is a leap year") else: print ("This is not a leap year")
Output:
An example of calculating the square root by user input
In this example, the square root of the given number is calculated and displayed after a user enters a valid number.
As the returned value by the input method is a string, this is first converted to a float and then sqrt() function is used to calculate the square root of the given number.
import math try: sqrt_num = float(input("Please Enter a number for calculating the square root? ")) except ValueError: print ("Only numbers are allowed - No strings or other characters!") else: print ("The Square root of",sqrt_num ,"=" ,math.sqrt(sqrt_num))
Output:
You can see that the code is placed inside the try-except block, so if a user accidentally enters a wrong value like a string, the program will not crash. Instead, a descriptive message is displayed, notifying the user about the mistake.
A demo of multiple input functions
As you run this simple program, it will ask the name, age and favorite color by the user. Upon entering all information, the program will display the information:
Y_name = input("What is your name? ") Y_age = input("What is your age? ") Y_color = input ("What is your favorite color?") print ("Welcome, %s." % Y_name) print ("Your age is %s" % Y_age) print ("Your favorite color is %s" % Y_color)
Output:
An example of using eval function with input
In the following example, the square of a given number is calculated and displayed by using the input function with eval().
I am just showing the simple use of eval() function while it is a big topic and requires its own chapter.
#The input with eval function example a_num = eval(input('Give a number to be squared? ')) b_squ = a_num*a_num print ("The square of the",a_num ,"is" , b_squ)
Result:
You may place an error handler like the above examples to avoid calculating a non-numeric value.