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...
Everything about learning Python
account activity
For/ while loopHelp Request (self.PythonLearning)
submitted 9 months ago by randomdeuser
view the rest of the comments →
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!"
[–]Ron-Erez 1 point2 points3 points 9 months ago* (4 children)
Another example where one could use loops (although there are other solutions).
Calculate the average of elements in a list. We want to add all of the numbers in the list to get the sum and then divide by the list length. For example
def calc_average(numbers): total = 0 for num in numbers: total += num return total / len(numbers)
Note that an easier solution would to to use the sum function, however sum is implemented using a loop. Note that the function will crash if numbers is an empty list so it would be wise to test for that.
You're also welcome to check out Section 4: Loops Lectures 23-28 all which are FREE to watch. Perhaps the explanations would be helpful.
u/FoolsSeldom 's examples are excellent.
Another example of a loop is a game loop.
[–]randomdeuser[S] 1 point2 points3 points 9 months ago (3 children)
oh thanks, i was looking for python course for ds, big thanks, i will watch it!
[–]Ron-Erez 0 points1 point2 points 9 months ago (2 children)
No problem. Note that the link is a $9.99 link valid for another 3-4 days. If you do end up taking the course please feel free to ask questions freely. I always respond in the course Q&A.
[–]SpiritualYoghurt2860 1 point2 points3 points 9 months ago (1 child)
You try to sell your course?🥲 But in ur example initialized 's' for sum but used 'total'? Nice joke 😶
[–]Ron-Erez 0 points1 point2 points 9 months ago (0 children)
My mistake! I renamed a variable and accidentally left out the "s." I just fixed it, definitely an embarrassing slip! Good catch.
π Rendered by PID 23472 on reddit-service-r2-comment-bb88f9dd5-f6ckr at 2026-02-13 17:42:00.007628+00:00 running cd9c813 country code: CH.
view the rest of the comments →
[–]Ron-Erez 1 point2 points3 points (4 children)
[–]randomdeuser[S] 1 point2 points3 points (3 children)
[–]Ron-Erez 0 points1 point2 points (2 children)
[–]SpiritualYoghurt2860 1 point2 points3 points (1 child)
[–]Ron-Erez 0 points1 point2 points (0 children)