2

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")
Pooja Jadhav
  • 33
  • 1
  • 1
  • 8
  • Looks like you're on the right track (with some mistakes) - Hint : what would be the output for : `num1=4`, `num2=3`, `num3=5`, `num4=2` and is it correct? Could you describe a little bit more what you want your output to be? – Charles Dupont May 11 '21 at 03:36
  • It is not clear what is the logic and expected output from the if-elif clauses. For example, if you get numbers 4, 7, 90, 26, the output will be `"Number 3 is greater"`. Is that correct? Or do you want the biggest number from the 4 numbers (should be 90)? – Gino Mempin May 11 '21 at 03:36
  • To make it clear, please [edit] to provide a description of what this code is *supposed* to do, and sample inputs and the expected outpus. – Gino Mempin May 11 '21 at 03:40

5 Answers5

2

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
>>> 
Jacob Lee
  • 4,405
  • 2
  • 16
  • 37
1

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")
Dushyant Singh
  • 1,270
  • 2
  • 9
  • 9
1

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))
Shubham Periwal
  • 2,198
  • 2
  • 8
  • 26
0
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")
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 14 '22 at 08:46
0
i am new to python .. please tell me how good this code is ?

n1 = int(input("Enter the number 1 = "))
n2 = int(input("Enter the number 2 = "))
n3 = int(input("Enter the number 3 = "))
n4 = int(input("Enter the number 4 = "))

if (n1>n2):
    if(n1>n3):
    if(n1>n4):
    print("N1 is greatest")
if (n2>n1):
    if(n2>n3):
    if(n2>n1):
    print("N2 is greatest")
if (n3>n1):
    if(n3>n2):
    if(n3>n4):
    print("N3 is greatest")
if (n4>n1):
    if(n4>n2):
    if(n4>n3):
    print("N4 is greatest")
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 01 '23 at 10:15