Write a python program to enter three numbers and display the largest number.Use nested if statement.
Answers
# change the values of num1, num2 and num3
# for a different result
num1 = 10
num2 = 14
num3 = 12
# uncomment following lines to take three numbers from user
#num1 = float(input("Enter first number: "))
#num2 = float(input("Enter second number: "))
#num3 = float(input("Enter third number: "))
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number between",num1,",",num2,"and",num3,"is",largest)
The easiest possible way to do this kind of a problem is to use the max() function in the list functions.
_______________________________________________________
Let's say we have an empty list and then we keep adding 3 inputs from the user to the list. I will use range() function to take inputs. It is much more stable.
_____________________________________________________
After taking the input, we will immediately add these numbers into a list called numbers and the call max(numbers) and we will be returned with the maximum number in the list.
___________________________________________________
CODE:
numbers = []
for i in range(3):
num = float(input(f'Enter your number({i}):'))
numbers.append(num)
print(f'The greatest number is {max(numbers)}')
________________________________________________
INPUT:
1,2,4
OUTPUT:
4