Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

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.

BeginnerPhase 1 - Level 1: Simple ConditionsExample 6 of 10
take-two-numbers-and-print-the-larger-one.py
1# Take two numbers as input
2num1 = float(input("Enter first number: "))
3num2 = float(input("Enter second number: "))
4
5# Find and print larger number
6if 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. 1

    Read num1 and num2 as floats.

  2. 2

    Test each strict larger-than relationship in order.

  3. 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.