0
How to complete this?
The given code defined a function called my_min(), which takes two arguments and returns the smaller one. You need to improve the function, so that it can take any number of variables, so that the function call works. def my_min(x, y): if x < y: return x else: return y print(my_min(8, 13, 4, 42, 120, 7))
10 Respuestas
+ 4
https://www.geeksforgeeks.org/args-kwargs-JUMP_LINK__&&__python__&&__JUMP_LINK/
#hope this helps you
+ 2
For comparison first declare a variable for comparison..
def my_min(*args):
r= args[0]
for num in args:
if num < r:
r = num
return r
print(my_min(8, 13, 4, 42, 120, 7))
+ 1
You can use kwargs for it
+ 1
Or *args will also be helpful.
+ 1
thanks! now much better) so i wrote this code. But what’s wrong here?(
def my_min(x, *y):
for i in y:
if x < i:
return x
else:
return i
print(my_min(8, 13, 4, 42, 120, 7))
0
yes so i used *args but still is wrong . look, here!
def my_min(x, *y):
if x < *y:
return x
else:
return *y
print(my_min(8, 13, 4, 42, 120, 7))
0
can anyon sow me the completely code i tried bu cant
0
but **kwargs will work for dictionaries)
0
Wangzi ur code is close, but it only checks the first variable.
def my_min(x, *y):
for i in y:
if x < i:
pass
else:
x = i
return x
0
Wangzi Here you go:
def my_min(*args): return min(args)
# Hope this helps