FAQ: Code Challenge: Control Flow - Max Number

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 posts were split to a new topic: What’s wrong with this logic?

3 posts were merged into an existing topic: What’s wrong with this logic?

2 posts were merged into an existing topic: What’s wrong with this logic?

2 posts were merged into an existing topic: What’s wrong with this logic?

4 posts were split to a new topic: Why should we use a naive approach?

2 posts were split to a new topic: Could this code be simplified?

2 posts were split to a new topic: Difference between elif and if statements?

This is my code which works, surely there is a more elegant way to solve this?

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!"
1 Like

2 posts were merged into an existing topic: Why should we use a naive approach?

2 posts were merged into an existing topic: Difference between elif and if statements?

A post was merged into an existing topic: Why should we use a naive approach?

3 posts were merged into an existing topic: Why should we use a naive approach?

2 posts were merged into an existing topic: What’s wrong with this logic?

2 posts were split to a new topic: Max number of negatives?

def max_num(num1, num2, num3):
  num_list = [num1, num2, num3]
  max_num = max(num_list)
  count_occ = num_list.count(max_num)
  if count_occ == 1:
    return max_num
  elif count_occ >= 2:
    return "It's a tie!"
  else:
    return "Error"

My own twist.

My solution is below, critiques are welcome :slight_smile:

I created a list first so that I could sort the list from greatest to least values. Then I compared the index of the first and second item in the list to see if there is a duplicate maximum value. If there was, it returned the required string. Otherwise, it returned the max of the tuple.

def max_num(num1, num2, num3):
  numlist = [num1, num2, num3]
  numlist.sort(reverse=True)
  if numlist[0] == numlist[1]:
    return "It's a tie!"
  else:
    return max(num1, num2, num3) 

3 posts were merged into an existing topic: What’s wrong with this logic?

Just wondering if there is a tie between two numbers what would the code be like if we want to return/print which two numbers were the same

Assuming we are writing an algorithm and not drawing upon any built-ins, we could first find the maximum, then iterate over the list again, this time looking for duplicates, and build a count of that value.