Reputation: 23
Im trying to make something like:
import time
if Action == 1:
while True:
money += 5
time.sleep(5)
and:
Action = input('>')
if Action = 2:
print('You now have $' + str(money) + '!')
so that in the background variable money keeps going up 5 every 5 seconds and whenever the user types in '2' it shows how much the variable money has.
ex.
background:
money = 5
5 secs
money = 10
5 secs
money = 15
3 secs
user: 2
You now have $15!
2 secs
money = 20
Upvotes: 0
Views: 21
Reputation: 650
Fast code (python 3.6):
import _thread
import time
scope = [dict(action=1, money=0)]
# Define a function for the thread
def money_handler ():
while True:
if scope[0]["action"] == 1:
scope[0]["money"] += 5
if scope[0]["action"] == 2:
scope[0]["money"] += 10
time.sleep(5)
print(scope[0]["money"], scope[0]["action"])
def action_handler():
while True:
time.sleep(5)
scope[0]["action"] = 2 if scope[0]["action"] == 1 else 1
# Create two threads as follows
_thread.start_new_thread(money_handler, ())
_thread.start_new_thread(action_handler, ())
Think about that as 2 separate program flows that interact only with scope
variable
Upvotes: 1