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))

5th May 2021, 10:51 AM
Wangzi
10 odpowiedzi
5th May 2021, 11:00 AM
Atul [Inactive]
+ 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))
5th May 2021, 11:28 AM
Aditya
Aditya - avatar
+ 1
You can use kwargs for it
5th May 2021, 10:57 AM
Atul [Inactive]
+ 1
Or *args will also be helpful.
5th May 2021, 10:59 AM
Atul [Inactive]
+ 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))
5th May 2021, 11:11 AM
Wangzi
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))
5th May 2021, 10:56 AM
Wangzi
0
can anyon sow me the completely code i tried bu cant
5th May 2021, 10:58 AM
Wangzi
0
but **kwargs will work for dictionaries)
5th May 2021, 10:59 AM
Wangzi
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
6th May 2021, 5:22 PM
madeline
madeline - avatar
0
Wangzi Here you go: def my_min(*args): return min(args) # Hope this helps
6th May 2021, 6:25 PM
Calvin Thomas
Calvin Thomas - avatar