1
number01 = 12
number02 = 20
number01 + 1
number02 + 2
print(number01) #prints :13
print(number02) #prints : 22

(reset all variables)

print(number01) #prints :12  
print(number02) #prints :12`

I have looked at other posts but they did not answer my question! How do I reset all variables to their default value? So that you can run your game multiple times.

2
  • Can you edit your post to put the program in a code block with proper new lines. What you've written is hard to understand. Commented Feb 21, 2016 at 6:54
  • Not sure if you made a typo but doing number01 + 1 will not add one to the value stored in number01. Perhaps you meant number01 += 1 Commented Apr 5, 2021 at 1:12

4 Answers 4

2

There is no default value in python. I do not know if any language at all has that construct. If you need to store the value of a variable while doing other operations with them, you need to use another temporary variable

number01 = 12
number02 = 20
print(number01) # 12
print(number02) # 20

old_number01 = number01
old_number02 = number02
number01 += 1
number02 += 2
print(number01) # 13
print(number02) # 22

number01 = old_number01
number02 = old_number02
print(number01) # 12
print(number02) # 20
Sign up to request clarification or add additional context in comments.

Comments

1

It seems like the numbers are reset to their original value if number01 is 12 again. Printing number01 + 1 should not change the variable. Constructs like the following would change the value:

number01 += 1

or

number01 = number01 + 1

Once changed, number01 = 12 would set it back.

If this does not address your question, please show the exact code, using 4 spaces before each code line.

Comments

1

For this I'd consider having the originals saved in a global variable and a function that sets the variables to this original value.

number01 = 12
number02 = 20

num1Or = number01  
num2Or = number02

def resetVar():
    global number01, number02
    number01 = num1Or
    number02 = num2Or

number01 += 1  
number02 += 2  

print(number01) 
print(number02) 

resetVar() 

print(number01)
print(number02) 

Comments

0

Just assign the original values to some global variables, and then don't change those variables.

original_number_01 = 12
original_number_02 = 02

// Do some stuff without changing either of the above...

print(original_number_01)
print(original_number_02)  

A program is just a sequence of instructions, and, from a bird's-eye view, it stores data if and only if you tell it to. And the only way to do so is through variable assignment, either explicit (writing a variable assignment yourself) or implicit (calling a function with side-effects).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.