Inputs

Inputs allow users to interact with your programs by entering data or making decisions.

Key Points

At National 5 there is only one type of input you need to know.

The input line can be changed depending on the data type.

Wrap the input line in brackets and state the data type before it to set the input to a data type.

Creating an Input

The most basic form of input line will only get data as a string.

The following line asks the user for their name and stores it as the variable ‘name’.

name = input("Enter your name: ")

Get a string from an input

The following line asks the user to enter a string. Whatever the user enters will then be used as a string.

name = str(input("Enter your name: "))

Get an integer from an input

The following line asks the user to enter an integer. Whatever the user enters will then be used as an integer, a whole number.

age = int(input("Enter your age: "))

Get a float from an input

The following line asks the user to enter a float. Whatever the user enters will then be used as a float, a decimal number.

price = float(input("Enter the items price: "))

National 5 Requirements

You should be able to create input lines that assign a variable.

You should be able to set the data type of the variable.