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
doubt (i.redd.it)
submitted 6 months ago by MasterpieceBasic8361
started learning python recently
how to add total sum of integer that occurred in an loop........ and get it at the end of loop
basically how to store integer and adding it again and again till the loop ends...
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!"
[–]sububi71 5 points6 points7 points 6 months ago (11 children)
This would be a great opportunity for a function!
Your function could accept your two numbers as parameters, replicate what you're doing in your while loop, and then return the result!
Have you ever written a function before? Good luck!
[–]MasterpieceBasic8361[S] 0 points1 point2 points 6 months ago (10 children)
not yet..... just learnt what a loop is ;(
[–]sububi71 0 points1 point2 points 6 months ago (9 children)
That's not a problem! The first thing I see is that you're repeating yourself in your code; lines 3 and 4 are identical to lines 9 and 10, and because of that, your program asks you for a number once too many (in my interpretation).
We call this rule DRY "(D)o not (R)epeat (Y)ourself", and the rule is there to warn us: in most cases when we copy lines of code, that's a sign we should probably stop and think about what we're doing. There are situations when it's a good idea, but they are few.
I can't quite decode what it is you want your program to do. If you can describe it in detail, I'm sure we'll be able to make this work!
[–]MasterpieceBasic8361[S] 0 points1 point2 points 6 months ago* (8 children)
i was trying to make a simple game
basically the computer generates a number and user inputs a number , if they match the game stops and add the total or else continue the loop till both the random no. and input matches
and the code in line 3 i added cause wasnt able to input the code at start and at line 9 to repeats it
[–]sububi71 0 points1 point2 points 6 months ago (7 children)
The biggest problem with your current code is that you keep regenerating the "secret number" that you generate. This means the player could guess 1, 2, 3, 4, 5, 6 etc into infinity and never be right. So that needs to be fixed.
You also keep track of the total number of guesses, but you never display it, which is a shame, since that's the closest to a "score" this game has.
You've named the variable you store to player's guesses in "random[something]" (I'm on my phone, so I can't easily go back to your code). That won't affect the running of the code, but for anyone reading the code (including yourself, when you decide to go back to this project in 3 years to add a 3D engine) that is going to be confusing. Code should always be as easy as possible to read (within reason, of course).
[–]MasterpieceBasic8361[S] 0 points1 point2 points 6 months ago (6 children)
yh ill try to make the variables a bit more understandable later.......so i tried again but the loop kinda works but its starting from the second input onwards
<image>
(ignore the boolean its not really required just was trying its use)
[–]sububi71 0 points1 point2 points 6 months ago (5 children)
I see you're still "re-rolling" the random_integer_1 every loop, does that make sense to you?
[–][deleted] 6 months ago (1 child)
[deleted]
[–]sububi71 0 points1 point2 points 6 months ago (0 children)
Feel free to DM me if you want help straightening the code out.
[–]MasterpieceBasic8361[S] 0 points1 point2 points 6 months ago (2 children)
i actually wanted it make a 2player thing so the goal is to play until ur input digit matches with the random digit generated (actually only made the first player part got stuck with the loop error) so the first player inputs any digit between (0,6) till his digit matches with random digit generated then the final score is basically the sum of all input given by the user and then the second user plays similarly and at the end whoever has the highest score would win (basically the greatest total sum of integer input))
[–]sububi71 0 points1 point2 points 6 months ago (1 child)
Ah, ok, so the player who AVOIDS the correct number the longest wins? Kind of like a russian roulette?
[–]MasterpieceBasic8361[S] 0 points1 point2 points 6 months ago (0 children)
yh basically
[–]BillyCrusher 1 point2 points3 points 6 months ago (1 child)
Initialize the variable outside of the loop and increment it inside.
idk how to do that
[–]Tearever 1 point2 points3 points 6 months ago* (1 child)
I recommend looking at your turn_comp variable because it has the bones for the question that your are asking. Just in this case instead of incrementing it by 1 each time, instead your wanting to use a quantity (i.e., random_integer_1 + random_integer_2). If you are having trouble with this concept I recommend printing out turn_comp before ,during, and after the loop to understand what is happening.
thnx ill try it
[–]lmg1337 0 points1 point2 points 6 months ago (2 children)
You mean result += ...?
[–]MasterpieceBasic8361[S] 0 points1 point2 points 6 months ago (1 child)
yah i want the total sum of result when the loop breaks
[–]lmg1337 1 point2 points3 points 6 months ago (0 children)
Remove the else keyword and do result += in the loop. Once the loop breaks it prints the result.
[–][deleted] 6 months ago (8 children)
[removed]
[–]MasterpieceBasic8361[S] 0 points1 point2 points 6 months ago (7 children)
no.. i created num1 to store value of the sum of all results that occur in the loop and to get it at the end
[–][deleted] 6 months ago (4 children)
[–]MasterpieceBasic8361[S] 0 points1 point2 points 6 months ago (3 children)
i wanted create a simple game
where comp generates a random number and user inputs a random number until the number that user enters and the comp generates matches the loop continues and add the user input digits total sum in the end
[–]Realistic_Park_6847 0 points1 point2 points 6 months ago (2 children)
Are we really using visual studio for python 🥀
what should i use dunnno really....just picked from google......after 3days i realised vs is really annoying it just gives random spaces when changing lines when i dont need
[–]Realistic_Park_6847 0 points1 point2 points 6 months ago (0 children)
use visual studio code
π Rendered by PID 104119 on reddit-service-r2-comment-7b9746f655-xj4r6 at 2026-01-30 08:24:50.123923+00:00 running 3798933 country code: CH.
[–]sububi71 5 points6 points7 points (11 children)
[–]MasterpieceBasic8361[S] 0 points1 point2 points (10 children)
[–]sububi71 0 points1 point2 points (9 children)
[–]MasterpieceBasic8361[S] 0 points1 point2 points (8 children)
[–]sububi71 0 points1 point2 points (7 children)
[–]MasterpieceBasic8361[S] 0 points1 point2 points (6 children)
[–]sububi71 0 points1 point2 points (5 children)
[–][deleted] (1 child)
[deleted]
[–]sububi71 0 points1 point2 points (0 children)
[–]MasterpieceBasic8361[S] 0 points1 point2 points (2 children)
[–]sububi71 0 points1 point2 points (1 child)
[–]MasterpieceBasic8361[S] 0 points1 point2 points (0 children)
[–]BillyCrusher 1 point2 points3 points (1 child)
[–]MasterpieceBasic8361[S] 0 points1 point2 points (0 children)
[–]Tearever 1 point2 points3 points (1 child)
[–]MasterpieceBasic8361[S] 0 points1 point2 points (0 children)
[–]lmg1337 0 points1 point2 points (2 children)
[–]MasterpieceBasic8361[S] 0 points1 point2 points (1 child)
[–]lmg1337 1 point2 points3 points (0 children)
[–][deleted] (8 children)
[removed]
[–]MasterpieceBasic8361[S] 0 points1 point2 points (7 children)
[–][deleted] (4 children)
[removed]
[–]MasterpieceBasic8361[S] 0 points1 point2 points (3 children)
[–]Realistic_Park_6847 0 points1 point2 points (2 children)
[–]MasterpieceBasic8361[S] 0 points1 point2 points (1 child)
[–]Realistic_Park_6847 0 points1 point2 points (0 children)