Conditions
In the lesson about operators we got to know logical operators. You can ask the computer for any value and it will tell you if our value is true or false.
See example: conditions
age = 44
print(age >= 18) # only for adults!
The result will be:
True
The forty-four-year-old is definitely older than an eighteen-year-old, so the result is the logical value "true".
if statements
Thanks to logical operations we can introduce conditions into our program. If the user's age is less than 18, tell him to come with parents.
In Python we execute it with the if statement. After the if keyword, the condition follows.
age = 15
if age < 18:
print(“Come with your parents!”)
See example: if statement
Pay attention to the colon and code block indentation which we know from the previous lesson! The rules are the same as for a loop.
This piece of code should be read as follows:
-
If age < 18:
-
write "Come with your parents!".
We already know how to turn out a teenager - now we should welcome an adult. We use the else clause for this.
age = 26
if age < 18:
print(“Come with your parents!”)
else:
print(“Welcome, have a good morning.”)
See example: using else
Else means "otherwise". The whole statement should be read as follows:
-
If age < 18:
-
write "Come with your parents!"
-
Otherwise:
-
Write "Welcome, have a good morning."
Pay attention to the colon and the indentation after else.
The last example will be a situation in which we want to treat a person younger than 18 years of age differently from an eighteen-year-old and an older person:
age = 18
if age < 18:
print(“Come with your parents!”)
else if age == 18:
print(“You’ve finally grown enough for our application.”)
else:
print(“Welcome, have a good morning.”)
See example: the use of else if
Take a look at the else if instruction. This is an instruction you should read: "If the previous condition is not met, check this one. This means that the whole thing should be read as follows:
-
If age < 18:
-
write "Come with your parents!".
-
if the previous condition is not met but the age is 18:
-
write "You've finally grown enough for our application."
-
Otherwise:
-
write "Welcome, have a good morning."
Task 1
We continue with our virtual fan club of The Beatles. Look at the code, you will find the beginning of the program there: an input command that asks you to enter the name of the musician, accepts the answer from the user and returns the answer to a variable.
Add code to check if the user's answer is "Paul McCartney". When the user enters the name, write "yes" back.
Go to task one: Task 1
Ready?
See solution: Solution to Task 1
Task 2
...and now check that the user has typed the Ringo Starr's instrument correctly. If the user has entered the correct instrument (drums), display the word "excellent!" on the screen, otherwise "this is not the right instrument!”.
You already have a piece of code that will retrieve information from the user. :)
Go to the second task: Task 2
Ready?
See solution: Solution to Task 2
Task 3
The Beatles have recorded 12 studio albums. Write a simple guessing game that will ask for this number. If the user enters a value less than 12, write "more", if the user enters a value greater than 12, write "less". In any other case, write "Bravo!".
Note: the input function will always return a string. The code that we have written retrieves the answer and tries to convert it into a number. If this fails, Python will report an error. Remember to enter the correct numbers :).
Go to task three: Task 3
Ready?
See solution: Solution to Task 3