Python Forum

Full Version: Executing 2 lines at the same time
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi I am trying to execute two lines at the same time. The lines are shown below. Either that or I need it to stop the option to input an answer after 3 seconds on line 24. Thankyou! Smile
import random
import time
Number_0_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Number_1_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
variable = 0
print('You have 3 seconds to answer each question')
time.sleep(3)
while True:                   #This needs to 
    time.sleep(1)             #Run While
    variable = variable + 1   #11 - 31 runs
for x in range(0,10):
    print('Ready...')
    time.sleep(1)
    print('Set...')
    time.sleep(1)
    print('Go!!!')
    time.sleep(1)
    Number_0 = random.choice(Number_0_list)
    Number_1 = random.choice(Number_1_list)
    print(Number_0, '*', Number_1)
    Answer = Number_0 * Number_1
    variable_record = variable
    while variable_record == variable_record + 3:
        InputAnswer = input("Answer Here: ")
    print(Answer)
    print('Did you get it right?')
    time.sleep(2)
    print("Now let's do it again")
    time.sleep(2)
    print('Ready for the next one?')
    time.sleep(2)
You need to play around with multithreading. Since variable is referenced in both loops, you should also reading into locks and thread safe data structures.
I posted yesterday a code snippet to ask for data with timeout in a console application. I think you can use this snippet for your case.