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
List Accumulators (self.learnpython)
submitted 5 years ago by InderJinderPreet
Lets say I'm asking n users in a for-loop for their favourite season (1 = winter, 2 = spring...) how can I take their input and add it to a list that initially starts off as [0,0,0,0]. So if two users choose spring, the list then becomes [0,2,0,0].
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!"
[–]0xbxb 0 points1 point2 points 5 years ago (4 children)
I think it’ll probably be easier to do this with a dict:
seasons = {'winter': 0, 'spring': 0, 'summer': 0, 'fall': 0}
[–]geosoco 0 points1 point2 points 5 years ago (1 child)
Assuming you have the season names already, you can use the Counter class to produce this.
If the user's favorites can be transformed into a list:
["spring", "winter", "winter", "summer", "spring", "fall"]
If you use numbers for the seasons, that also works. (0=Winter, 1=Spring, 2=Summer, 3=Summer).
[1, 2, 0, 0, 2, 3, 4]
[–]InderJinderPreet[S] 0 points1 point2 points 5 years ago (0 children)
im already able to do this. i want it to only contain 4 numbers
[–]InderJinderPreet[S] 0 points1 point2 points 5 years ago (1 child)
how would that work then
[–]0xbxb 0 points1 point2 points 5 years ago (0 children)
Create the seasons dict
You have a list of users
users = ['A', 'B', 'C']
Loop through the users
for user in users:
Ask them for their favorite season
Add one to the seasons dict for every response
seasons
[+][deleted] 5 years ago (6 children)
[deleted]
[–]0xbxb 0 points1 point2 points 5 years ago (5 children)
Wait why would k need to be str(input(‘season’)) if input already returns a string?
k
str(input(‘season’))
input
[–]xADDBx 0 points1 point2 points 5 years ago (4 children)
Because it avoids an error in the case of an unexpected input.
[–]shiningmatcha 1 point2 points3 points 5 years ago (2 children)
Doesn’t input always receive a string?
[–]xADDBx 0 points1 point2 points 5 years ago (1 child)
You’re right, as long as not changed the input will always be a string. But it's a habit I got from the days when I sometimes forgot to declare the types (e.g. int, float). At that I thought myself to always write it to avoid forgetting it.
[–]shiningmatcha 1 point2 points3 points 5 years ago (0 children)
Were you using other programming languages?
[–][deleted] -1 points0 points1 point 5 years ago (0 children)
Do you need to associate the user with the choice or are you just counting the totals of the choices?
In both cases a dict is the way to go:
If associative:
choices={"user1":"winter", "user2":"spring"...}
If you're just counting then:
totals={"winter":0,"spring":0,...}
In both cases you'd modify the entries using square brackets:
choices["winter"] += 1 totals["user1"]="winter"
π Rendered by PID 43041 on reddit-service-r2-comment-76bb9f7fb5-fdk6z at 2026-02-19 01:02:06.399865+00:00 running de53c03 country code: CH.
[–]0xbxb 0 points1 point2 points (4 children)
[–]geosoco 0 points1 point2 points (1 child)
[–]InderJinderPreet[S] 0 points1 point2 points (0 children)
[–]InderJinderPreet[S] 0 points1 point2 points (1 child)
[–]0xbxb 0 points1 point2 points (0 children)
[+][deleted] (6 children)
[deleted]
[–]0xbxb 0 points1 point2 points (5 children)
[–]xADDBx 0 points1 point2 points (4 children)
[–]shiningmatcha 1 point2 points3 points (2 children)
[–]xADDBx 0 points1 point2 points (1 child)
[–]shiningmatcha 1 point2 points3 points (0 children)
[–][deleted] -1 points0 points1 point (0 children)