Take Two Numbers and Print the Larger One
Compare two entered values and print the larger one, or report a tie. A beginner Python walkthrough of float conversion, strict comparisons, and equality.
1# Take two numbers as input2num1 = float(input("Enter first number: "))3num2 = float(input("Enter second number: "))45# Find and print larger number6if num1 > num2:7 print(f"Larger number: {num1}")8elif num2 > num1:9 print(f"Larger number: {num2}")10else:11 print("Both numbers are equal")
Output
Enter first number: 10 Enter second number: 20 Larger number: 20.0
In this one
- Compare float values
- Handle ties explicitly
- Read an f-string
How it works
- 1
Read num1 and num2 as floats.
- 2
Test each strict larger-than relationship in order.
- 3
Print the larger value or the equality message.
Edge cases
What if both inputs are 7?
What if both inputs are 7?
The program prints that both numbers are equal.
Can both values be negative?
Can both values be negative?
Yes. For -2 and -9, -2 is larger.
What's going on
Finding the larger of two values also needs a plan for a tie. Both responses become floats before the program compares num1 with num2.
The first test selects num1 only when it is strictly greater. The second selects num2 in the opposite case. If neither succeeds, the numbers are equal. With 10 and 20, the formatted message contains 20.0.
What if both inputs are 7? The program prints that both numbers are equal.
Can both values be negative? Yes. For -2 and -9, -2 is larger.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Why is 20 printed as 20.0?
Pick an answer to check it.
Question 2
What if both inputs are 7?
Pick an answer to check it.
Questions people ask
What if both inputs are 7?
The program prints that both numbers are equal.
Can both values be negative?
Yes. For -2 and -9, -2 is larger.
Why is 20 printed as 20.0?
The input was converted to float.
๐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