*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))
5/8/2021 4:15:03 AMBryson S.
5 AnswersNew Answer
U also have to change the body m=args for n in args: if n < m : m=n return m
Your code worked fine. 1) Try to use *args alone and access first variable with args. And remaining with args[1:] 2) Or try *argv instead of *args
Update your expected output. I am not able to understand your required output.
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!
Thank you, it still does not pass on the system but I learned a 4th way to solve it. So that's great!