Take Three Numbers and Print the Largest
Find the largest of three numbers without sorting. Students follow paired Python comparisons, tie handling with >=, and a separate final print statement.
1# Take three numbers as input2num1 = float(input("Enter first number: "))3num2 = float(input("Enter second number: "))4num3 = float(input("Enter third number: "))56# Find largest number7if num1 >= num2 and num1 >= num3:8 largest = num19elif num2 >= num1 and num2 >= num3:10 largest = num211else:12 largest = num31314print(f"Largest number: {largest}")
Output
Enter first number: 10 Enter second number: 25 Enter third number: 15 Largest number: 25.0
In this one
- Compare three candidates
- Allow tied maxima
- Store a result before printing
How it works
- 1
Read the three numeric values.
- 2
Test num1, then num2, against both competitors.
- 3
Store the winning candidate and print largest.
Edge cases
What if all three are equal?
What if all three are equal?
The first branch chooses num1, which is still a largest value.
What if two share the maximum?
What if two share the maximum?
The earlier successful branch wins; the displayed maximum is unchanged.
What's going on
Each candidate must be at least as large as both other values. num1 is tested first using two comparisons connected by and.
If num1 fails, the program tests num2. Otherwise num3 must be a largest value. Using >= allows ties, so two equal maxima do not break the logic. The sample stores 25.0 in largest before printing it outside the branch chain.
What if all three are equal? The first branch chooses num1, which is still a largest value.
What if two share the maximum? The earlier successful branch wins; the displayed maximum is unchanged.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Why use and between comparisons?
Pick an answer to check it.
Question 2
What if all three are equal?
Pick an answer to check it.
Questions people ask
What if all three are equal?
The first branch chooses num1, which is still a largest value.
What if two share the maximum?
The earlier successful branch wins; the displayed maximum is unchanged.
Why use and between comparisons?
A candidate must beat or tie both others.
๐Related Content
- ๐
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- ๐
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- ๐ป
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum