Python code challenges - Advanced control flow - if statement syntax

Hi all,

why am I getting a syntax error on the if statement?

Exercise is this one, fifth challenge.

Thank you

def max_num(num1, num2, num3):

  nlist = [num1,num2,num3]

  z = max(nlist)

  if ((num1 != num3 and num1 != num3) and(num 2 != num3)):

    return z

  else:

    return "It's a tie!"

Welcome to the forums!

Double-check this section. Remember that and should be surrounded by a space on either side. Without the space on the right, Python thinks that you’re trying to call a function since the syntax for a function call is function_name().


I’ve made sure it’s separated by a space, but still stuck on syntax error :neutral_face:

Double-check how you write num2 on that line. See anything extra you put there?

1 Like

On another note, what would happen if you called max_num(5, 2, 2)? We get It's a tie! even though we should get 5. Can you spot what went wrong and fix it?

Syntax resolved, thanks.

Yes, upon getting the code to run for the first time I see that it’s not functional in the way I wanted it to be.
It should be running the print only if the num values are the same and both are the highest values.

1 Like
def max_num(num1, num2, num3):

  nlist = [num1,num2,num3]

  z = max(nlist)

  if (z==num1) and (z==num2):

    return "It's a tie!"

  elif(z==num2) and (z==num3):

    return "It's a tie!"

  elif(z==num3) and (z==num1):

    return "It's a tie!"

  

  else:

    return z

Got it to work. Hoped to make more use of the list but kept running into a wall there.

Nice! There is a way to make more use of the list, and at the same time reduce the number of conditionals you use. Can you figure out how (hint provided below if you need it)?

What if the list was conveniently in order of least to greatest, or greatest to least?