Python: User Input

Variables, output, and math were covered in the previous pages. Here, we will add to this the ability to get input from the user.

Input from the user via the keyboard is accomplished with the Python "input" command. The input command can accept user input into three main data types:

  • string
  • integer
  • float

Example: String and integer user input

In the example below, Bill and 32 are entered as user input. This example also shows how the comma symbol adds a space in the output - in this example not wanted.


	print("Welcome to this input example program.")
	name = input("What is your name? ")
	age = int(input("What is your age? "))
	print("Welcome", name, ". You are", age, "years old.")
	

Output:

  Welcome to this input example program.
  What is your name? Bill
  What is your age? 32
  Welcome Bill . You are 32 years old.


It is important to note the following points:

  • The user can enter any type of data (string, integer or float) into a string user input (such as the name variable above). However, if a number (integer or float) is entered, it is being stored as a string and not a number.
  • The user can enter either a float or an integer into a float user input. However, it will be stored as a float either way. So, if 15 is entered into a float user input, it will be stored as 15.0. If the user enters a string into a float user input, the program will crash. Review the input validation section of this web site to see how to prevent this with error handling.
  • The user must an integer into a integer user input or the program will crash. Review the input validation section of this web site to see how to prevent this with error handling.

Example: Use of + symbol in output

The example below demonstrates how the + sign can be used to seperate strings without a space. The + sign cannot be used with numbers (unless you cast them first).


	print("Welcome to this input example program.")
	name = input("What is your name? ")
	age = int(input("What is your age? "))
	print("Welcome", name + ". You are", age, "years old.")
	

Output:

  Welcome to this input example program.
  What is your name? William
  What is your age? 28
  Welcome William. You are 28 years old.


Example: User Input and Math – Multiplying two integer numbers

In the example below, the user inputted 2 and 3.


	print ("This program will multiple two integer numbers.")
	x = int(input("Enter the first integer number:"))
	y = int(input("Enter the second integer number:"))
	total = x * y
	print (x, " x ", y, " = ", total)
	

Output:

  This program will multiple two integer numbers.
  Enter the first integer number:2
  Enter the second integer number:3
  2 x 3 = 6


Example: Print statement that rounds float numbers to two digits to right of decimal

In the example below, the user inputted 2.2 qand 3.


	print ("This program will multiple two float numbers.")
	x = float(input("Enter the first integer number:"))
	y = float(input("Enter the second integer number:"))
	total = x * y
	print (x, " x ", y, " = %.2f" % total)
	

Output:

  This program will multiple two float numbers.
  Enter the first integer number:2.2
  Enter the second integer number: 3
  2.2 x 3.0 = 6.60


Example: User input into variables with float data type

In the example below, the user inputted 3.5 and 2.


	x = float(input("Enter value of x: "))
	y = float(input("Enter value of y: "))
	total = x * y
	print("Total = ", total)
	

Output:

  Enter value of x: 3.5
  Enter value of y: 2
  Total = 7.0


Example: Calculating a Baseball Batting Average

In the example below, the user inputs 2 and 5, which are stored as integers. The resulting math operation creates a battingAverage variable with a float data type.


	hits = int(input("Enter the player’s number of hits: "))
	bats = int(input("Enter the player’s number of times at bat: "))
	battingAverage = hits / bats
	print("The player's batting average is ", battingAverage)
	

Output:

  Enter the player’s number of hits: 2
  Enter the player’s number of times at bat: 5
  The player's batting average is 0.4


Example:


	# Description:  Salary Program
	# Date:         May 1, 2018
	# Author:       J. Smith
	TAX_RATE = .25

	# User input
	firstName = input("Please enter your first name: ")
	salary = float(input("Please enter your gross salary: "))
	hireYear = int(input("Please enter the year you were hired: "))

	# Calculations
	netSalary = salary * (1 - TAX_RATE)

	# Output
	print("Hello,", firstName + ". You were hired in", hireYear)
	print("Your net salary is", netSalary)
	print("Your net salary is ${:,.2f}".format(netSalary))
	

Output:

  Please enter your first name: William
  Please enter your gross salary: 40000
  Please enter the year you were hired: 2000
  Hello, William. You were hired in 2000
  Your net salary is 30000.0c   Your net salary is $30,000.00


Example:



	

Output:

  


Example: String user input

In the example below, the user inputted William via the keyboard. This example also shows how an input command can be combined (nested inside) with a print command.


	print(input('What is your name? '))
	

Output:

  What is your name? William
  William


Example: String user input

The example below is another way in which an input command can be nested inside a print statement.


	print("Hello", input("What is your name? "))
	

Output:

  What is your name? William
  William


Example: User input with concatenation


	name = input("Please enter your name:\t\t")
	age = int(input("Please enter " + name + "'s age: \t"))
	print(name, "is", age)
	

Output:

  Please enter your name:    Tim
  Please enter Tim's age:      29
  Tim is 29