Input and decisions
input() asks the user to type something, and gives it to you as a string:
name = input("What is your name? ")
print(f"Hello, {name}!")
Here, answers come from the input box above the Run button, one per line.
Even if the user types digits, input() gives text. Turn it into a whole number with int(...):
age = int(input("How old are you? "))
if, elif, else
temperature = 31
if temperature > 30:
print("Hot! Drink water.")
elif temperature > 20:
print("Warm.")
else:
print("Cool.")
- The condition ends with a colon
:. - The code that belongs to a branch is indented (4 spaces). Indentation is how Python knows what is inside the
if. elif("else if") is only tried when the branches above it were false. The first true branch wins.
Comparisons: == (equal), != (not equal), >, <, >= (at least), <= (at most).
Your task
Ask for a mark out of 100 and print its grade:
| Mark | |
|---|---|
| 80 or more | Grade: A |
| 60 to 79 | Grade: B |
| 50 to 59 | Grade: C |
| below 50 | Grade: F |
Change the number in the input box and run it a few times.