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 ( ) below!
Agree with a comment or answer? 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 !
catower
Split this topic
September 19, 2019, 10:21pm
3
3 posts were split to a new topic: What’s wrong with this logic?
catower
Split this topic
September 19, 2019, 10:25pm
6
3 posts were merged into an existing topic: What’s wrong with this logic?
catower
Split this topic
September 19, 2019, 11:04pm
9
2 posts were merged into an existing topic: What’s wrong with this logic?
catower
Split this topic
September 19, 2019, 10:28pm
10
2 posts were merged into an existing topic: What’s wrong with this logic?
catower
Split this topic
September 19, 2019, 10:30pm
12
4 posts were split to a new topic: Why should we use a naive approach?
catower
Split this topic
September 19, 2019, 10:44pm
17
2 posts were split to a new topic: Could this code be simplified?
catower
Split this topic
September 19, 2019, 10:48pm
19
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
catower
Split this topic
September 19, 2019, 10:54pm
22
2 posts were merged into an existing topic: Why should we use a naive approach?
catower
Split this topic
September 19, 2019, 11:03pm
24
2 posts were merged into an existing topic: Difference between elif and if statements?
catower
Split this topic
September 19, 2019, 11:04pm
30
A post was merged into an existing topic: Why should we use a naive approach?
catower
Split this topic
September 19, 2019, 11:05pm
32
3 posts were merged into an existing topic: Why should we use a naive approach?
catower
Split this topic
September 19, 2019, 11:07pm
35
2 posts were merged into an existing topic: What’s wrong with this logic?
catower
Split this topic
September 19, 2019, 11:09pm
37
2 posts were split to a new topic: Max number of negatives?
My solution is below, critiques are welcome
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)
catower
Split this topic
November 26, 2019, 1:44am
41
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
mtf
January 9, 2020, 7:36pm
43
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.