1.6 Input data from Keyboard
input
We used the print function to display program output. The input function is used to read user input from the keyboard. When the built-in-function input is called, the program stops and waits for the user to type from the keyboard. When the user presses Return or Enter, the program resumes and input returns what the user typed as a string data type by default.
- Click
+ Codeto add a code-cell - Type
x = input - Type
print(x) - Click
play - Type the number 2 using your keyboard and press
enter.
See code below
# x = input()
x = "2" # ← web edition: the value the book's own run typed in
print(x)
Show expected output
2
2Note: The value of input entered from the keyboard is stored in x
It is a good practice to print a prompt telling the user what to input. For example, input("what is your name?") will make it clear to the user what to enter. See code below.
- Click
+ Code - Type
x = input("What is your name? ") - Type
print("Your name is", x) - Click
play - Enter
Donfrom your keyboard
# x = input("What is your name? ")
x = "Don" # ← web edition: the value the book's own run typed in
print("Your name is",x)
Show expected output
What is your name? Don
Your name is DonNote: The value of input entered by the user is stored in x. That is x = Don
Input Examples
Example: Write a Python program that accepts an integer and computes and prints the square-root of . See code below.
# x = input("Enter any integer number: ") #ask the user for a number
x = "5" # ← web edition: the value the book's own run typed in
x = int(x) # converts the input from string to integer (casting)
y = x**.5
print("The square-root is", y)
Show expected output
Enter any integer number: 5
The square-root is 2.23606797749979Example: Write a Python program which asks the user for a radius of a circle and displays the area using the formula . See code below.
# r=input("Enter the radius of a circle: ") #ask the user for radius
r = "6" # ← web edition: the value the book's own run typed in
r=float(r) # converts the string to float
import numpy # we need the number pi
area = numpy.pi*r**2
print("The area of the circle is", area,'square units')
Show expected output
Enter the radius of a circle: 6
The area of the circle is 113.09733552923255 square unitsExample: Write a Python program to calculate adult body mass index for a given weight in pounds and height (feet and inches) using the formula, . See code below.
# w=input("Enter weight in pounds:") #ask the user for a number
w = "60" # ← web edition: the value the book's own run typed in
# h1 = input("Enter height feet only:")
h1 = "5" # ← web edition: the value the book's own run typed in
# h2 = input("Enter inches:")
h2 = "2" # ← web edition: the value the book's own run typed in
w,h1,h2 = float(w),float(h1),float(h2) # converts the number to float
bmi = 703*(w/(h1*12+h2)**2)
print("The BMI is", bmi)
Show expected output
Enter weight in pounds:60
Enter height feet only:5
Enter inches:2
The BMI is 10.972944849115505Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.