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:
The new commands used in this program and others that may be useful. Select them below to learn more:
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 logical 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?
You can have as many conditions in one statement as you need. Use brackets () for order of precedence.
The following Boolean operators can be used to join multiple conditions into a single selection statement that returns either True or False:
and
Both conditions must be True.
or
One of the conditions must be True.
You can have as many conditions in one statement as you need. Use brackets () for order of precedence.
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 the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.
Run your code to check that your program has met the success criteria and produces the required output.
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