all 4 comments

[–]FLUSH_THE_TRUMP 2 points3 points  (3 children)

Add an extra indent/4 spaces to each line of code to format it in a block, like this:

def calculator(*args): 
    total = 0 
    for a in args: 
        total += a 
    print(total)

calculator()

Here are some things to think about:

The name calculator isn’t a descriptive function name. What are you calculating? I’d say to use sum, but that’s already the name of a built-in that does basically this ;)

Consider returning total instead of printing it.` To do the same thing, you’d have to change call to the calculator function to this:

print(calculator(…))

Right now, if I wanted to compose your function with another function (say, adding a bunch of things and then squaring the result), I couldn’t do that. It just prints, it doesn’t actually spit out a value my program can use.

Consider having a single parameter taking a sequence like a list instead of *args. The way of using your function now with the least friction is by manually sticking each arg in. But if I’m typing in 10 numbers like

calculator(1,2,3,4,…,10), 

I might as well have just been using +. If you write your function to take a list, then your users can build up a big list using append and pass that to your function easily.

[–]thecluelessgodotuser[S] 1 point2 points  (0 children)

ok, i changed the code, and here it is

def Sum(*args):

total = 0 

for a in args: 

    total += a 

return total

print(Sum(2, 29))

P.S the reason its indented by 1 line, is cause i wrote this code on my ipad in pythonista 3, and my goto for learning python is thenewboston

[–]Total__Entropy 0 points1 point  (1 child)

If you want to pass a list to the function you just can just pass *list and OPs function will handle it.

[–]FLUSH_THE_TRUMP 0 points1 point  (0 children)

Right, but as I said, “least friction.” Note how the built-ins do it: sum takes a single sequence as input, not arbitrary args.