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
  • 41
  • 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

4 Answers4

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
  • 604
  • 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,697
  • 1
  • 5
  • 16
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,065
  • 1
  • 6
  • 17
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