'How to increment a variable without it going back to 0
This is my code and every time I run it, it just goes back to 0 so it never adds it to the bank, i know it’s a stupid question but i’d appreciate some help. Thank you!
import random as r
# get the start time
st = time.time()
bank=0
def work ():
z=r.randint(1000,8000)
global bank
bank=bank+z
jobs=[]
y=r.choice(jobs)
e=f"You worked as {y} and made {z} and you now have {bank} in your account"
return e
print(work())
time.sleep(1)
# get the end time
et = time.time()
# get the execution time
elapsed_time = et - st
print('Execution time:', elapsed_time, 'seconds')```
How would I add it back to the original bank variable?
Solution 1:[1]
Frankly speaking, I don't quite understand what you mean, but I guess you may want to write like this
import random as r
import time
# get the start time
st = time.time()
bank=0
while True:
def work ():
z=r.randint(1000,8000)
global bank
bank=bank+z
jobs=['testjob']
y=r.choice(jobs)
e=f"You worked as {y} and made {z} and you now have {bank} in your account"
return e
print(work())
time.sleep(1)
# get the end time
et = time.time()
# get the execution time
elapsed_time = et - st
print('Execution time:', elapsed_time, 'seconds')
stopflag = input('if you want work again ,plsea send 1 : ')
if stopflag != '1' :
break
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | yang jiang |
