Menu

Programming Fundamentals

Input

Contents

  1. Storing Input in Variables
  2. Conversion Is Not Validation

An input statement is used to store what the user types in a variable. The variable name is written on the left of an equals sign, and the message that asks the user for data — the prompt — is written inside the brackets of the input call.

In the statement instrument = input("Which instrument do you play? "), the name instrument is written without quotes, because it is a variable being created, not text. The prompt is a string, so it does need quotes.

To store the value, put the input call on the right of the equals sign and the variable on the left. The left of the equals sign must be a variable name, because that is the place that receives the value. Writing input("Which instrument do you play? ") = instrument is not valid, because a function call is not a variable.

The program pauses until Enter is pressed

Once the prompt appears, the program stops and waits. Nothing else runs until the user has typed a value and pressed Enter. At that moment the text typed becomes a value and is stored in the variable, ready for later lines to use.

# The answer is kept in the variable so it can be used later
instrument = input("Which instrument do you play? ")
print("Great choice,", instrument)

Output when the user types guitar:

Which instrument do you play? guitar
Great choice, guitar

A space is not added after the prompt for you, which is why the prompt above ends with a space — without it the user's typing would appear straight after the question mark.

The result is lost if it is not stored

If the input call is written on its own with no variable to receive the value, the value is created but no name refers to it, so it disappears as soon as the line finishes. No later line can print it, calculate with it or check it.

# Only the book title is kept; the page count is not stored
book = input("Book title: ")
input("Number of pages: ")
print("You borrowed", book)

Output when the user types The Lost Whale, 240:

Book title: The Lost Whale
Number of pages: 240
You borrowed The Lost Whale

Finally, never use input as a variable name. Names like book or shelf are free to use, but naming a variable input replaces the name of the input function, so later calls to input() will not work.

Converting text with int() or float() changes its data type. It does not check that the number makes sense, or that it falls inside an allowed range.

Converting the text 62 with int() works because 62 is a whole number, and a whole number is all the function is asked for. Whether 62 is a reasonable answer is a separate question that the conversion never considers.

bikes_text = input("Number of bikes (1 to 4): ")
bikes = int(bikes_text)
cost = bikes * 12  # cost per bike in pounds
print(cost)

Output when the user types 62:

Number of bikes (1 to 4): 62
744

The prompt asks for 1 to 4 bikes, yet typing 62 still converts without complaint. The program multiplies and prints a price for 62 bikes, because int() only answers whether the text can become a whole number — the answer is yes, so the value is used.

A conversion fails when the text cannot represent the required type — for int(), the word lots cannot become a whole number — and then the program stops with an error. Even that is a judgement about the type, not about whether the value is sensible.

Question askedConversionValidation
Can this text become a number?Checks itDoes not check it
Is the number in the allowed range?Ignores itChecks it
62 typed where only 1 to 4 is allowedAcceptedRejected

So a value can be converted perfectly and still be nonsense. Checking input against rules, such as a range check, is validation, and that belongs with defensive design.

Also useful