How can I stop these variables from resetting while still being able to loop them?

how to end a while loop
while loop javascript
while loop format
numpy while loop
how to write a for loop as a while loop
how to use a while loop
multiple while loops
how to end a while loop in python

Here's part of my code:

def dice_game():
     dice_window = Tk()
     dice_window.geometry('450x450')
     player1_score = 0 
     player2_score = 0
     def player1_turn():
         player1_num1 = random.randint(1, 6)
         player1_num2 = random.randint(1, 6)
         player1_score = player1_num1 + player1_num2
         player1_total = player1_num1 + player1_num2
         if (player1_total % 2) == 0:
             player1_score = player1_score + 10
         else:
             player1_score = player1_score - 5
         player1_result = Label(dice_window, text = ( "Player 1 got", player1_num1, "and", player1_num2, "and their total score is:", player1_score))
         player1_result.pack()
     for x in range(1, 6):
         player1_turn()

I've tried putting the loop inside the player1_turn () function and in the dice_game() function but the outcome is always the same. How can I keep the 2 players score without resetting them every time this section loops 5 times?

Let's break this down. I can see that player1_turn() is nested because you want to have access to dice_window. But there is a better way!

First separate the functions. player1_turn needs a dice window, and it looks like the thing you want to save is player1_score, so we'll return that.

def player1_turn(dice_window):
    player1_num1 = random.randint(1, 6)
    player1_num2 = random.randint(1, 6)
    player1_score = player1_num1 + player1_num2
    player1_total = player1_num1 + player1_num2
    if (player1_total % 2) == 0:
        player1_score = player1_score + 10
    else:
        player1_score = player1_score - 5
    player1_result = Label(dice_window, text = ( "Player 1 got", player1_num1, "and", player1_num2, "and their total score is:", player1_score))
    player1_result.pack()
    return player1_score

Now onto the game:

def dice_game():
    dice_window = Tk()
    dice_window.geometry('450x450')

    # we're not using x, so we should make it clear that this is a loop of 5
    scores = []
    total = 0
    for x in range(5):
        player1_score = player1_turn(dice_window)
        # now what to do with the score?
        scores.append(player1_score)  # save it?
        print(player1_score)  # print it?
        total += player1_score  # add it up
    print(scores)
    print(total)

Readings in Computer Architecture, We will discuss only while-loops here, using this version of the function ip which is of the loop contains expressions with free variables j, s, A, and B. Two of them, j and s, It is possible to think of loop constants as if they too, were circulating, using the trivial For the moment, ignore the operators labeled D and D-reset. I am a total newbie and am self teaching myself. I worte a program that lets the user guess a random number and then asks if if they want to play again.\ Here is the code: #include <iostream> #include <cstring> #include <stdlib.h> #include <ctime> #include <conio.h> using namespace std; int main

Since you are assigning player1_score and player2_score inside the body of player1_turn() function, it makes player1_score & player2_score local variable of function player1_turn().

So, if you are using python3 you can define player1_score and player2_score as non-local variable explicitly using nonlocal keyword.

The code will look like this.

def dice_game():
     dice_window = Tk()
     dice_window.geometry('450x450')
     player1_score = 0 
     player2_score = 0
     def player1_turn():
         nonlocal player1_score, player2_score
         player1_num1 = random.randint(1, 6)
         player1_num2 = random.randint(1, 6)
         player1_score = player1_num1 + player1_num2
         player1_total = player1_num1 + player1_num2
         if (player1_total % 2) == 0:
             player1_score = player1_score + 10
         else:
             player1_score = player1_score - 5
         player1_result = Label(dice_window, text = ( "Player 1 got", player1_num1, "and", player1_num2, "and their total score is:", player1_score))
         player1_result.pack()
     for x in range(1, 6):
         player1_turn()

# END

For python 2, there is a workaround that you can store the player1_score & player2_score in a dict like score = {'player1_score': 0, 'player2_score': 0}

Handbook of Food Engineering Practice, The variable that represents the greatest disturbance to the primary loop and is the Reset action may not be desirable since it has the potential to slow down that prevent an unsafe or otherwise undesirable process state from being reached . If the reset action is allowed to saturate ( windup or winddown ) with the  I have been brushing up on my C lately and I have come across a problem that has left me puzzled while working through an old c book. The program is copied as it is in the book. Basically the problem is that the variable no_els is being reset to 0 each time despite no code to reset it and the code used to initialise it is out side the do while

Well, the code as it is has your for loop inside your dice_game() function. As you can see you're declaring the scores to zeroes in that function. So, to call the for loop you need to call dice_game(), which resets the scores to zero again.

Maybe you should try taking that for loop outside of the function dice_game(), and in the end, the score will be preserved. Or you can take the declarations

player1_score = 0 
player2_score = 0

outside the function, as global variables.

How can I reset my variables after each iteration and then save them , Learn more about iteration, sample, data series, reset variables, loop, preallocation. And after choosing it, at the block parameter I added:. 1 Answer1. Active Oldest Votes. 1. If you mean the variable stop1, it's true every loop it's set to 1. If you don't want this, make it global, set it in setup (and change in loop when needed). E.g. int stop1 = 0; void setup () { stop1 = 1; } void loop () { // Use stop and/or change it } share.

Variable input to MATLAB function resets involuntarily inside while , The function runs a while loop and I insert an if-check inside it to break out of the loop. case, but I need to have the check in order to be able to get out of the loop. event to happen and stop when it did, like a variable changing state. if-​elseif check somehow causes the value it is checking to change,  I am declaring an int with bigger scope than a problematic while() loop. At the end of each single iteration of the while() loop, that variable seems to get reset to 0 without my doing. I am using no forking/multithreading here. Here is the symptomatic code:

Why are my variables being reset every time I continue a while loop , So it acts as if it's still on the same line, even though I have delcared it to increment at row++ and column++ . Why do my integer variables keep  I have a Bash script where I want to count how many things were done when looping through a file. The count seems to work within the loop but after it the variable seems reset.

Variable resetting when the code loops, I need it to retain the value of the variable and continue to add onto it in the following loops, but my variable resets when it returns. To be clear. It is because when you call a function, you pass a copy of the variable, not the  Yea, but you'll need to store it's original value in another variable. Or if it's known at compile time just manually reset it back to that in the code. EDIT: This is assuming the variable has scope outside of the loop. If not, that variable is gone forever once the loop exits.

Comments
  • Look up more information about the scope of a nested function. Then, you have to add to the score, not reinitialize it every time. That will reset the value.
  • Make a minimal reproducible example and edit it in to your post.
  • So how should I put this together on my code? Just have them after each other?
  • Yep, just one after the other. If "Now onto the game:" was removed from my post.
  • It works kind of. I'll tweak it a little but what you've given me has fixed my core problem. Thanks a lot!