1

Im struggling to fix my code for a prompted assignment. Sometimes it gives the correct answers, other times it doesn't. For whatever reason, the input "12, 18, 4, 9" gives a max of 12?

def max_number(num1, num2, num3, num4):
    if (num1 > (num2 and num3 and num4)):
        return num1
    elif (num2 > (num3 and num4)):
        return num2
    elif num3>num4:
        return num3
    else:
        return num4
    
    
def min_number(num1, num2, num3, num4):
    if num1<(num2 and num3 and num4):
        return num1
    elif num2<(num3 and num4):
        return num2
    elif num3<num4:
        return num3
    else:
        return num4
    
    
if __name__ == '__main__':
    num1 = int(input())
    num2 = int(input())
    num3 = int(input())
    num4 = int(input())
    print('Maximum is', max_number(num1, num2, num3, num4))
    print('Minimum is', min_number(num1, num2, num3, num4))
Park
  • 2,446
  • 1
  • 16
  • 25
glow
  • 13
  • 2
  • 1
    Just glancing at this, do you mistake `and` for addition? And is just a boolean operator. That is to say `2 and 1` will return 1. – Spencer Feb 20 '22 at 07:13
  • Hint: what exactly do you expect `(num2 and num3 and num4)` to mean by itself? What do you expect to happen if you compare *the result of that* to `num1`? Because that is necessarily what happens; you cannot make the `<` "distribute" across the `and`s. – Karl Knechtel Feb 20 '22 at 07:48
  • That said: `max` and `min` are built in. What exactly is the intent of this code? – Karl Knechtel Feb 20 '22 at 07:49
  • Thank you all for the help! It was a class project that required we worked around all of the built-in information. I'm very new to programming so I appreciate the feedback and guidance! – glow Feb 20 '22 at 20:21

2 Answers2

2

I think this is the syntax you're looking for:

def min_number(num1, num2, num3, num4):
    if num1 < num2 and num1 < num3 and num1 < num4:
        return num1
    elif num2 < num3 and num2 < num4:
        return num2
    elif num3<num4:
        return num3
    else:
        return num4

and is a bool compare when used in an if statement like this, it doesn't chain if statements as I think you believe. In other words each statement in between the and needs to be complete, if that makes sense.

Spencer
  • 1,931
  • 1
  • 21
  • 44
0

In addition to the answers offered which touch on the behavior of and when used with two integers, you may also wish to calculate whether a value is less than all of another set of numbers using something like:

all(num1 < n for n in [num2, num3, num4])

Or checking to see that num1 is greater than num2, num3, and num4:

all(num1 > n for n in [num2, num3, num4])
Chris
  • 26,361
  • 5
  • 21
  • 42
  • This is a nice, clean way of solving the problem. However I think that comprehensions might be a bit above his head atm... @glow check out python list comprehension if you want to understand what is happening here, this would be a very "pythonic" way of achieving what you want. – Spencer Feb 20 '22 at 19:21
  • What's shown here are related to list comprehensions, but are actually "generator expressions." – Chris Feb 20 '22 at 19:50
  • Will do that for sure! I appreciate the specific guidance!! – glow Feb 20 '22 at 20:22
  • Chris, Good point. @glow There is actually a good example/explanation here that I just found: https://www.python.org/dev/peps/pep-0289/ About halfway through "Rationale" they give an example of min/max/sum. – Spencer Feb 20 '22 at 22:00