Which is the largest of three numbers?
Select the button below to open the Python program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.
Watch this video to learn about the new concepts shown in the program:
Questions to think about with this program to check your understanding:
Explain the purpose of `and` in line 7 and what this line of code is checking.
Both condtions must be True for the indented code underneath the statement to be executed. Both number1, number2 and number1, number3 must be equal. This line checks if all the numbers are the same. Note it is not necessary to check number2 against number3.
Explain why double equals `==` is required in line 7 and not a single equals `=`.
Double equals ask the question, “are they both equal?” A single equals means make them equal. We want to check the value of number1 against number2 and number3, not set number1 to be the same as number2.
Change the program so that it:
Enter the first number: 3
Enter the second number: 6
Enter the third number: 9
The largest is number: 3
Use these resources to learn about new commands for this level and to help you meet the success criteria.
means else if. If the previous condition is False then try this condition instead. You can have as many elif commands as you need.
A selection with multiple conditions requires each condition to be enclosed in parenthesis (brackets).
The following operators can be used in conditions. They all return True or False:
Is equal to. E.g. `x == y` means is x equal to y?
Is not equal to. E.g. `x != y` means is x different to y?
Is less than or equal to. E.g. `x <= y` means is x less than or equal to y?
Is greater than or equal to. E.g. `x >= y` means is x greater than or equal to y?
The following Boolean operators can be used to join multiple conditions into a single selection statement that returns either True or False:
Both conditions must be True.
One of the conditions must be True.
You can have as many conditions in one statement as you need. Use brackets () for order of prescedence.
Run the unit tests below to check that your program has met the success criteria.
Enter the first number: 70
Enter the second number: 20
Enter the third number: 10
The largest is number: 1
Enter the first number: 90
Enter the second number: 120
Enter the third number: 20
The largest is number: 2
Enter the first number: 30
Enter the second number: -8
Enter the third number: 99
The largest is number: 3