The Student Room Group

def functions

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;



def max(a, b):
if a > b:
return a
elif b > a:
return b
else:
return None
what to you mean - you don't know how to test it?
Reply 2
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.
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
Reply 4
but what if i want the user to input the values
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)
Original post by FatimaAli10
but what if i want the user to input the values

did it work? :smile:
Reply 7
Original post by Bill Nye
did it work? :smile:


sorry i was at uni.. came home didnt try yet.. will soon n then I'll let you know
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 :smile:
Reply 9
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?

Quick Reply

Latest

Trending

Trending