def functions Watch

FatimaAli10
Badges: 5
Rep:
?
#1
Report Thread starter 4 weeks ago
#1
Define a new function max2() that takes two numbers as parameters and returns the largest. You should use an if else statement inside the function definition. (It is true that Python has a max() function built in, but writing one yourself is good practise). Write a simple program to test the function.
Thats what i have done so far;
not sure how to do it further
any help would be appreciated;

Code:
def max(a, b):
    if a > b: 
        return a 
    elif b > a: 
        return b 
    else: 
        return None
0
reply
Bill Nye
Badges: 21
Rep:
?
#2
Report 4 weeks ago
#2
what to you mean - you don't know how to test it?
0
reply
FatimaAli10
Badges: 5
Rep:
?
#3
Report Thread starter 4 weeks ago
#3
(Original post by Bill Nye)
what to you mean - you don't know how to test it?
i do know but there isnt anything to be tested since its just a code. i wanted to add values. i dont know how to perform this task overall.
0
reply
Bill Nye
Badges: 21
Rep:
?
#4
Report 4 weeks ago
#4
(Original post by FatimaAli10)
i do know but there isnt anything to be tested since its just a code. i wanted to add values. i dont know how to perform this task overall.
to add values type max(a, b) replacing a and b with the numbers you want to compare
0
reply
FatimaAli10
Badges: 5
Rep:
?
#5
Report Thread starter 4 weeks ago
#5
but what if i want the user to input the values
Posted on the TSR App. Download from Apple or Google Play
0
reply
Bill Nye
Badges: 21
Rep:
?
#6
Report 4 weeks ago
#6
(Original post by FatimaAli10)
but what if i want the user to input the values
could you have something like
x = int(input("Enter the first number: "))
y = int(input("Enter the second number: "))

max(x, y)
0
reply
Bill Nye
Badges: 21
Rep:
?
#7
Report 4 weeks ago
#7
(Original post by FatimaAli10)
but what if i want the user to input the values
did it work?
0
reply
FatimaAli10
Badges: 5
Rep:
?
#8
Report Thread starter 4 weeks ago
#8
(Original post by Bill Nye)
did it work?
sorry i was at uni.. came home didnt try yet.. will soon n then I'll let you know
Posted on the TSR App. Download from Apple or Google Play
0
reply
Bill Nye
Badges: 21
Rep:
?
#9
Report 4 weeks ago
#9
(Original post by FatimaAli10)
sorry i was at uni.. came home didnt try yet.. will soon n then I'll let you know
Oh sorry
1
reply
trident3
Badges: 9
Rep:
?
#10
Report 4 weeks ago
#10
There are two bugs in your function.

First bug: it's not called max2.

Second bug... here's a hint : what does the builtin function max(33, 33) return?
1
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

University open days

  • University of Roehampton
    Department of Humanities; Department of Drama, Theatre and Performance; Department of Social Sciences; Department of English and Creative Writing Undergraduate
    Wed, 20 Feb '19
  • Keele University
    Postgraduate Open Afternoon Postgraduate
    Wed, 20 Feb '19
  • Manchester Metropolitan University
    Postgraduate Open Day Postgraduate
    Wed, 20 Feb '19

Do unconditional offers make teenagers lazy?

Yes (113)
58.85%
No (79)
41.15%

Watched Threads

View All