Python Forum
Executing 2 lines at the same time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Executing 2 lines at the same time
#1
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)
Reply
#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.
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  More elegant way to remove time from text lines. Pedroski55 6 3,909 Apr-25-2021, 03:18 PM
Last Post: perfringo
  Assistance with running a few lines of code at an EXACT time nethatar 5 3,227 Feb-24-2021, 10:43 PM
Last Post: nilamo
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,766 Aug-10-2020, 11:01 PM
Last Post: medatib531
  Two lines of code at the same time? OTO1012 1 2,266 Mar-04-2019, 05:32 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020