*args practice question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

*args practice question

Hi I am stuck on a what feels like a simple practice Python question. I think I solved it in multiple ways but the system is still not passing me so I must be missing something. Could use a hand. Thank you! Question: *args 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. Remember, *args can be accessed inside the function as a tuple. starter code: def my_min(x, y): if x < y: return x else: return y print(my_min(8, 13, 4, 42, 120, 7)) My Solution 1: def my_min(x, *args): for y in args: if x < y: print(x) else: print(y) my_min(8, 13, 4, 42, 120, 7) My Solution 2: def my_min(x, *args): result = [] for y in args: if x < y: result.append(x) else: result.append(y) return result print(my_min(8, 13, 4, 42, 120, 7)) My Solution 3: def my_min(x, *y): result = "" for num in y: if x < num: result += str(x) else: result += str(num) return result print(my_min(8, 13, 4, 42, 120, 7))

8th May 2021, 4:15 AM
Bryson S.
Bryson S. - avatar
5 Answers
+ 5
U also have to change the body m=args[0] for n in args: if n < m : m=n return m
8th May 2021, 6:13 AM
Oma Falk
Oma Falk - avatar
+ 3
Your code worked fine. 1) Try to use *args alone and access first variable with args[0]. And remaining with args[1:] 2) Or try *argv instead of *args
8th May 2021, 5:02 AM
Rinaldo Nikilson
Rinaldo Nikilson - avatar
0
Update your expected output. I am not able to understand your required output.
8th May 2021, 4:44 AM
Rinaldo Nikilson
Rinaldo Nikilson - avatar
0
Thank you! The question is vague. It asks to fix the starter code so it can take “any number of variables” and so “the function works”. I think i did both but within the course, it fails to pass the check. It runs in Jupyter notebook and in Atom. If my solution looks good to you, I’ll just move on I guess. I was afraid I was missing something. Thank you, again!
8th May 2021, 4:53 AM
Bryson S.
Bryson S. - avatar
0
Thank you, it still does not pass on the system but I learned a 4th way to solve it. So that's great!
8th May 2021, 1:01 PM
Bryson S.
Bryson S. - avatar