SIMULATION
Complete the function get_max(a, b) that returns the larger of two numbers. If they are equal, return either one.
def get_max(a, b):
# TODO: Return the larger of a and b
pass
Suggested Answer: A
Get Premium Foundations of Programming (Python) (E010) Questions
as Interactive Web-Based Practice Test or PDF
Real Exam Format
Detailed Explanations
Instant Access
Get Your Practice Test
==========
Step 1: The function receives two parameters: a and b.
Step 2: Compare the two values using an if statement.
Step 3: If a is greater than or equal to b, return a.
Step 4: Otherwise, return b.
Correct code:
def get_max(a, b):
if a >= b:
return a
else:
return b
Simplified correct code:
def get_max(a, b):
return max(a, b)
Example:
print(get_max(10, 20))
print(get_max(30, 15))
print(get_max(5, 5))
Output:
20
30
5
21 comments · newest first
DE
Denny
2 days ago
Me too, clear and concise.
BR
Brunilda
7 days ago
I like this type of question.
DE
Delsie
12 days ago
Exactly, either one is fine.
FR
Frederic
17 days ago
Both work for equal values too.
DE
Denny
23 days ago
if b >= a: return b
DE
Denny
28 days ago
Or:
BR
Brunilda
1 month ago
if a > b: return a
BR
Brunilda
1 month ago
I’d write:
DE
Delsie
1 month ago
Just need to compare values.
FR
Frederic
2 months ago
I feel confident about it.
DE
Denny
2 months ago
I agree, simple logic.
BR
Brunilda
2 months ago
This question is straightforward.
FR
Frederick
2 months ago
This is basic stuff, should be easy to implement!
PA
Paulene
2 months ago
Isn't it weird to return either if they're equal?
RI
Rima
2 months ago
Wait, what if both numbers are negative?
JE
Jerry
3 months ago
Totally agree, it's straightforward!
KA
Karan
3 months ago
Just need to compare a and b.
AN
Andree
3 months ago
I believe we just need a simple if statement to compare a and b, but I hope I remember the syntax correctly!
IV
Ivette
3 months ago
I practiced something like this before, but I can't recall if I need to handle types explicitly.
LA
Layla
3 months ago
This seems straightforward, but what if both numbers are negative? Does it change how we compare them?
EM
Emiko
4 months ago
I think I remember using the max() function in a similar practice question, but I'm not sure if that's allowed here.