0
def max_num(num1, num2, num3):
  if num1 > num2 and num3:
    return num1
  elif num2 > num1 and num3:
    return num2
  elif num3 > num1 and num2:
    return num3

print(max_num(-10, 0, 10)) # first check, returns 0 (wrong)
print(max_num(-10, 5, -30)) # second check, returns 5 (right)
print(max_num(-5, -10, -10)) # third check, returns -5 (right)

I'm only something like 3 days into learning my first programming language (Python), and I was given a task to compare 3 numbers and return the largest. The 2nd and 3rd print checks are correct, as 5 and -5 are the largest out of their 3 numbers. However, the first check returns 0, which is obviously not the largest number, 10 is.

StockAZ89
  • 51
  • 1
  • 7
  • 1
    Does this answer your question? [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – kaya3 Apr 18 '20 at 02:57

5 Answers5

4

While this does answer your question, it does not fulfill the intent of your class.

max([1, 3, 2]) # 3

when using the and operator you should think of it as this.

if (condition) and (condition):

so use if num1 > num2 and num1 > num3:

James Powis
  • 609
  • 4
  • 16
2

It is due to the fact that you can't factorize like this your condition. The condition:

if num1 > num2 and num3:
    ...

really means "If num1 is greater than num2 and num3 is different from 0". Indeed, Python tries to convert num3 into a boolean. If num3 is an empty list, an empty string, 0 or False, then it is evaluated to False. Otherwise, it is evaluated to True. What you really wanted to write is, for instance:

if num1 > num2 and num1 > num3:
    ...
Tristan Nemoz
  • 1,844
  • 1
  • 6
  • 19
1

Your logical statement is not executing in the way you are thinking. In num1 > num2 and num3 it will check whether num1 is greater than num2 and num is 0 or not. you can change your conditons to num1 > num2 and num1 > num3. Try the following:

def max_num(num1, num2, num3):
  if num1 > num2 and num1 > num3:
    return num1
  elif num2 > num1 and num2 > num3:
    return num2
  elif num3 > num1 and num3 > num2:
    return num3

print(max_num(-10, 0, 10)) # returns 10
print(max_num(-10, 5, -30)) # returns 5
print(max_num(-5, -10, -10)) # returns -5

or like this:

def max_num(num1, num2, num3):
    if num1 > num2:
        if num1 > num3:
            return num1
        else:
            return num3
    else:
        if num2 > num3:
            return num2
        else:
            return num3

print(max_num(-10, 0, 10)) # returns 10
print(max_num(-10, 5, -30)) # returns 5
print(max_num(-5, -10, -10)) # returns -5
Prudhvi
  • 1,095
  • 1
  • 7
  • 18
1

Using the reserved word "and" in python in a condition, returns True only if both sides of it are True. An Integer is considered True if it's not zero. for Example:

num1 = 5
num2 = 1
num3 = 0
return num1 > num2 and num2 > num3 # returns True because all the conditions are true
return num1 > num2 and num1: # returns True because num1>num2 and num1 is not 0
return num1 > num2 and num3: # returns False because one of the conditions is False becuase num3 is 0

If so, the conditions you've sent are considered like:

if num1 > num2 and num3: # if num1 > num2 and num3 is not 0 

elif num2 > num1 and num3: # if num2 > num1 and num3 is not 0

elif num3 > num1 and num2: # if num3 > num1 and num2 is not 0

Therefore, the first one returns 0 because in the last condition you're checking if num2 is not 0 but it is.

A simple way to solve it:

def max_num(num1, num2, num3):
  if num1 > num2 and num1 > num3:
    return num1
  elif num2 > num1 and num2 > num3:
    return num2
  return num3

print(max_num(-10, 0, 10)) # returns 10
print(max_num(-10, 5, -30)) # returns 5
print(max_num(-5, -10, -10)) # returns -5
0

I am new to coding and This is how i did it

       number_1= int(input("Enter first number"))
       number_2= int(input("Enter second number"))
       number_3= int(input("Enter third number"))
       if number_1 > number_2 and number_1 > number_3:
         print("the largest number", number_1)
       elif number_2 > number_1 and number_2 > number_3:
         print("The largest number is", number_1)
       elif number_3 > number_1 and number_3 > number_2:
         print("The largest number is",number_1)


       Enter first number 70
       Enter second number60
       Enter third number50

       the largest number 70
Meduxa
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 11 '23 at 02:34