Find greatest number entered by four users

Question:

I am learning python. I need to know, is this also valid way to get an output?
Following code should give me highest user input( highest number among the four numbers)

num1 = int(input("Enter number 1: ")) #user 1
num2 = int(input("Enter number 2: ")) #user 2
num3 = int(input("Enter number 3: ")) #user 3
num4 = int(input("Enter number 4: ")) #user 4

allNum = {num1, num2, num3, num4}
print("All numbers: ",allNum)
if(num1 > num2 and num1 > num4) :
    print("User 1's number  is greater")  
elif(num2 > num3) :
    print("User 2's number  is greater")
elif(num3 > num4) :
    print("User 3's number  is greater")
elif(num4 > num2) :
    print("User 4's number  is greater")
else :
    print("done")
Asked By: Pooja Jadhav

||

Answers:

With your current method of directly comparing each number, you would do something like this:

if num1 > num2 and num1 > num3 and num1 > num4:
    ...
elif num2 > num1 and num2 > num3 and num2 > num4:
    ...
...

Luckily, there is an easier way! You can use Python’s built-in max() function, which returns the maximum value of a sequence. That would look something like this:

maximum = max(num1, num2, num3, num4)
if num1 == maximum:
    ...
elif num2 == maximum:
    ...
...

The above method works fine, but what if you wanted to use 5 numbers? What about 6? 20? The more numbers you add, the more messy it gets. Each additional number of numbers, you would add another elif statement, which gets tedious. So I would also recommend using some loops and a dictionary for gathering, storing, and comparing the user input.

>>> nums = {}
>>> for i in range(4):
    nums[i+1] = int(input(f"Enter number {i+1}: "))

    
Enter number 1: 4
Enter number 2: 2
Enter number 3: 8
Enter number 4: 10
>>> print("All numbers:", list(nums.values()))
All numbers: [4, 2, 8, 10]
>>> for k, v in nums.items():
    if v == max(nums.values()):
        print(f"Number {k} is greater")
        break

    
Number 4 is greater
>>> 
Answered By: Jacob Lee

You can index and max to solve this.

a = []
for i in range(4):
    a.append(int(input(f"Enter number {i+1}: ")))
print(f"Number {a.index(max(a)) + 1} is greater")
Answered By: Dushyant Singh

Trying to keep it as simple as possible for you to understand

num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))
num3 = int(input("Enter number 3: "))
num4 = int(input("Enter number 4: "))

#Store all numbers in list. 
allNum = [num1, num2, num3, num4]

#Get the max value of all numbers in that list
maxNum = max(allNum)

#To get the position of this value, use list.index() function
maxIndex = allNum.index(max(allNum))

#Lets say your numbers are [3,5,4,1]. .index() would give you "1" because the index starts from 0 to length of list -1. 
#In this case, it would be 0-3. So, if you want `Number 2` is greater instead, then you just add 1 to the maxIndex.

print("Greatest number is number "+str(maxIndex+1))
Answered By: Shubham Periwal
This is the code i wrote .. I'm just learning python

n1 = int(input("Enter 1st Number: "))
n2 = int(input("Enter 2nd Number: "))
n3 = int(input("Enter 3rd Number: "))
n4 = int(input("Enter 4th Number: "))

if (n1>n2) and (n1>n3) and (n1>n4):
    print("n1 is Greater")
elif (n2>n1) and (n2>n3) and (n2>n4):
    print("n2 is Greater")
elif (n3>n2) and (n3>n1) and (n3>n4):
    print("n3 is Greater")
else:
    print(n4,"is Greater")
Answered By: Hasan Feroz
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.