-2

I have written this code for a class and the outcomes are correct, but I did not realize that I needed to incorporate a for-loop into the function. The exact words from the problem are "Hint, use a variable number of arguments (*values) and access them with a for loop." I don't really understand this and I think all I need to do is add a for loop somehow.

I haven't tried anything yet because i'm really bad with for-loops and don't want to mess up the code that I have. Anything helps. This is my code so far:

def values(num1 , num2 , num3):
    sum = num1 + num2 + num 3
    ave = sum / 3
    x = max (num1 , num2 , num3)
    y = min (num1 , num2 , num3)
    product = num1 * num2 * num3
    return sum , ave , x , y , product

sum , ave , x , y , product = values(2 , 2 , 2)
print(sum)
print(ave)
print(x)
print(y)
print(product)


6
2.0
2
2
8
Aspen
  • 1
  • 2
  • You can use `*nums` instead of `num1 , num2 , num3` as arguments. It will pass in any values you input into the function as a tuple. You can then iterate over that tuple (which will be named `nums`). – Joe Patten Jan 24 '19 at 00:24

1 Answers1

0

I'm not really familiar with python so im not 100% syntactically but the general idea is that the function can take N number of arguments and you can loop through each value, instead of limiting yourself to 3.

for example to compute the sum would be something like

def sum(*values):
  sum = 0

  for value in values:
    sum = sum + value

  return sum

Alt95
  • 71
  • 3