Getting input
The ability to get input from the user allows us to make our programs interactive i.e. they can respond to what the user enters. When we get input from the user we must store it somewhere. This means whenever we request input
we must provide a variable which will be assigned the value input. The basic input command can be seen below.
name = input("Enter your name: ")
In the above line of code, name is the variable that will be assigned the input and "Enter your name: " is the prompt that will appear to the user so they know what to enter.
The syntax rules for the basic input statement are:
- It starts with a variable and the assignment operator(=)
- input is in lower case
- There is a prompt in a string or f-string
to tell the user what to enter
- The prompt is surrounded by speech marks
Case conversion
You can convert a string to UPPERCASE, lowercase or Sentence Case using the methods below.
Case to convert to |
Description |
Method |
lowercase |
Converts all text to lowercase |
.lower() |
uppercase |
Converts all text to uppercase |
.upper() |
title case |
First letter of each word capitalised, the rest lowercase |
.title() |
By converting on input, you can save yourself the problem of having to check for different cases the user might have entered e.g.
guess = input(“What is the capital of England? ”).lower()
Regardless of how the user typed their response it will be stored as lowercase, meaning we only have to check for 1 possibly correct response. Otherwise they may have entered - London, LONDON or london. We will be looking at
how to check answers soon when we cover selection.