FAQ: Code Challenge: Control Flow - Max Number


#1

This community-built FAQ covers the “Max Number” exercise from the lesson “Code Challenge: Control Flow”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Max Number

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#3

Trying to figure out why my code doesn’t work. It works great for the first 3 test calls but returns None for the tie. Here is the code:

def max_num(num1, num2, num3):
  if num1 > num2:
    if num1 > num3:
      return num1
  if num2 > num1:
    if num2 > num3:
      return num2
  if num3 > num1:
    if num3 > num2:
      return num3
  else:
    return "It's a tie!"

It doesn’t look like it in the post, but the first test for each numx variable is indented 2 spaces with the following line indented another 2 spaces. The else is indented 2 spaces at the same level as the initial if.

I also changed the 2nd and 3rd ‘main’ ifs to elif. Doing that added another None return for the test call where num3 was the largest (1st test call)
I’m mostly wondering why my first attempt did not work for the tie.


#4

None is a value displayed by python when there is no explicit return. This is useful to understand

using if or elif has quite large implications on how your program behaves, you need to think about the logic, break it down in steps

in the code you posted, this test case fails:

print(max_num(2, 3, 3))

why? Because else only runs when if num3 > num1: is false, but if is true, so else won’t run.


#5

Thank you! I appreciate the help.


#6

So i wrote this code:

def max_num(num1, num2, num3):
  if num1 > num2 and num2 > num3:
    return num1
  elif num1 < num2 and num2 > num3:
    return num2
  elif num1 < num2 and num2 < num3:
    return num3
  else:
    return "It's a tie!"

but its giving me an error because it wont pick up a negative sign in one of the prints:

print(max_num(-10, 0, 10))
# should print 10
print(max_num(-10, 5, -30))
# should print 5
print(max_num(-5, -10, -10))
**# should print -5 but it prints This is a tie**
print(max_num(2, 3, 3))
# should print "It's a tie!"

#7

the problem is actually here:

if num1 > num2 and num2 > num3:

lets say we have: max_num(8, 4, 6)

then 8 should be returned, but this isn’t happening, because number two isn’t larger then number three. So are you sure you got your conditions right? Write down for yourself in plain text what should happen here


#8

Try this code
def max_num(num1, num2, num3):
if num1 > num2 and num1 > num3:
return num1
elif num2 > num3 and num2 > num1:
return num2
elif num3 > num2 and num3> num1:
return num3
else:
return “It’s a tie!”


#9
def max_num(num1,num2,num3):

#create a list within the three given arguments:
  myList = [num1,num2,num3]

#Check if the length of myList is different than the set of it (wich means it contains double numbers) and returns "Its a tie!" if it's different:
  if len(myList) != len(set(myList)):
    return "It's a tie!"

#Otherwise return the max() value of the given arguments:
  else:
    return max(num1,num2,num3)

#10

I wrote this

def max_num(num1,num2,num3):
if num1> num2 and num3:
return num1
elif num2> num1 and num3:
return num2
elif num3> num2 and num1:
return num3
else:
return “It’s a tie!”

I’m wondering why it prints “It’s a tie!”? If numx isn’t as large as numy and numz should it only return numx? and all the others fail?


#11

We need to perform the comparison on both sides of and.

if a > b and a > c

#12

Just did this problem, and while I had little issue solving it I can’t help but think this solution is very, well, clunky.

Given a set of number does anyone have the computationally fastest way to solve it? Or would it just be a coders choice of which sort algorithm to use?

I feel like this could be done recursively but I’m garbage at recursive and don’t know.

To any code gods could get back to me that would be Great!


#13

If that is all that a beginner was taught there would be a shortage of programmers. Ideally, we start at the bottom of the curve with what may well be the slowest code, but the most educational. Show a learner an advanced solution and they begin to believe that naive approaches have no value, which we know to be categorically false.

The intuitive way would be to sort, but the naive way would be to compare. Best that we exploit all that the naive approach can reveal before we toss out the baby with the bath water. Let the learner develop their own intuition.


#15

I’m kind of having the same issue, but mine does not return “it’s a tie” and returns instead the identical biggest number


#16

We will need to see your code in order to make a determination of any issue. Please post it in a reply.


#17

Not sure if people are still looking for the answers but this seems to have worked for me:
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
elif num3==num2 and num3>num1:
return “It’s a tie!”
elif num2==num1 and num2>num3:
return “It’s a tie!”
elif num3==num1 and num3>num2:
return “It’s a tie!”

If anyone has any thought about writing this code in a more efficient way, please do feel free to reply.


#18

That’s a lot of comparisons which is the first signal there must be a simpler way. Consider,

Step 1. Make the larger of the first two take the second position.

num1, num2 = num2, num1 if num1 > num2

Step 2. Make the larger of the last two take the last position.

num2, num3 = num3, num2 if num2 > num3

Step 3. Return num3

return num3