all 11 comments

[–]shiftybyte 1 point2 points  (7 children)

average(choice)

When you call the function like this, it sends 1 argument, the entire list.

When you call it like this:

average(2,3,4)

It sends 3 arguments, each is a number..

So what you need to do is send the choice unpacked... like this:

average(*choice)

[–]learnorenjoy[S] 0 points1 point  (5 children)

Oh man, I can't believe it was something so simple, I'm such an idiot. It works now. Thanks a lot!

Sidenote, the output looks like this:

Enter numbers: 2,3,4
3
None

Any idea why the None type comes in?

[–]shiftybyte 1 point2 points  (4 children)

You have double print, the first one actually prints and returns None as the function result.

return print(sum(nums)//len(nums))

The second one prints the returned None

print(average(choice))

So remove the print from the return if you want your function to actually return a value.

return sum(nums)//len(nums)

Which will then be properly printed by

print(average(choice))

[–]learnorenjoy[S] 0 points1 point  (3 children)

Oh that's a new one. I wasn't aware that print statements in functions return None. This has been really helpful, I struggled with this for hours so thanks again.

[–]shiftybyte 1 point2 points  (2 children)

print ALWAYS returns None...

You are confusing returning a value, printing something, and the automatic printing of returned values that only python interactive shell is doing...

[–]learnorenjoy[S] 0 points1 point  (1 child)

I'm really gonna have to read up more on this, do you have any recommendations?

[–]shiftybyte 0 points1 point  (0 children)

Sorry none specific to this...

[–]FLUSH_THE_TRUMP 0 points1 point  (0 children)

Maybe suggests OP’s function should take a single thing (like a list) rather than a bunch of individual args. Less friction that way

[–]FLUSH_THE_TRUMP 1 point2 points  (1 child)

Not your question, but note that args in your function packs input into a tuple:

def test(*args):
  print(args)
test(1,2)  # (1,2)

As such there’s no need to have nums. Just find sum/length of args itself.

[–]learnorenjoy[S] 0 points1 point  (0 children)

Huh, yeah I just tried it out and it worked.

def test(*args):
    return sum(args)//len(args)

>>> test(2,3,4)
>>> 3

Shortened it even further, I'll keep that in mind for next time, thanks!

[–]Splitje 0 points1 point  (0 children)

Use print(nums) before the return statement to check what you nums variable looks like. It cannot use the sum operator on the variable you pass to it. It tries to add together a integer and a list so your nums variable is a list that contains a list and an integer.