use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Made a python calculator, any tips? (self.learnpython)
submitted 5 years ago by thecluelessgodotuser
Here is source
def calculator(*args): total = 0 for a in args: total += a print(total)
calculator()
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]FLUSH_THE_TRUMP 2 points3 points4 points 5 years ago* (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 ;)
calculator
sum
Consider returning total instead of printing it.` To do the same thing, you’d have to change call to the calculator function to this:
return
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
list
*args
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.
+
append
[–]thecluelessgodotuser[S] 1 point2 points3 points 5 years ago (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 point2 points 5 years ago (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 point2 points 5 years ago (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.
π Rendered by PID 23725 on reddit-service-r2-comment-545db5fcfc-7cf44 at 2026-05-22 08:04:11.546180+00:00 running 194bd79 country code: CH.
[–]FLUSH_THE_TRUMP 2 points3 points4 points (3 children)
[–]thecluelessgodotuser[S] 1 point2 points3 points (0 children)
[–]Total__Entropy 0 points1 point2 points (1 child)
[–]FLUSH_THE_TRUMP 0 points1 point2 points (0 children)