Thread: Basic threading

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    78
    Rep Power
    2

    Basic threading


    Hey, I'm teaching myself threading and would love some tips from you guys.

    The code below isn't working properly(doesn't print out the seconds left when I'm inputting), I think the input function might cause the problem.

    Is there anyway around this, or a better way to do this?



    Code:
    #Imports modules
    import time
    import threading
    import random
    
    #Clears the screen
    print("\n"*80)
    
    #Creates a thread class
    class cTimer(threading.Thread):
        def __init__(self, iSecondsLeft):
            super(cTimer, self).__init__()
            self.iSecondsLeft = iSecondsLeft
            self.bRun = True
        def run(self):
            self.mTimer()
        def mStop(self):
            self.bRun = False
        def mTimer(self):
            for i in range(self.iSecondsLeft, 0, -1):
                if self.bRun == False:
                    return
                print("You have" + str(i) + "Seconds left to defuse the bomb\n")
                time.sleep(1)
            print("BOOOOOOOOOOOOOOM")
    
    
    def fPanel(iCode):
        iInput = 0
        while iInput != iCode:
            iInput = int(input("please input your combination"))
            if iInput == iCode:
                print("Congratulations, you defused the bomb")
                oTimer.mStop()
                return
            elif iInput < iCode:
                print("the number is bigger then that!")
            elif iInput > iCode:
                print("the number is smaller then that!")
    
    #Creates a timer(in a new thread), and starts counting down
    oTimer = cTimer(10)
    oTimer.start()
    
    #Lets the user guess the code to stop the timer
    iCode = random.randrange(10)
    fPanel(iCode)
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    I ran the program and got this output, showing that I defused the bomb.
    Code:
    $ python3 p.py
    
    You have 10 seconds left to defuse the bomb
    please input your combinationYou have 9 seconds left to defuse the bomb
    You have 8 seconds left to defuse the bomb
    1
    the number is bigger then that!
    please input your combination2
    the number is bigger then that!
    please input your combinationYou have 7 seconds left to defuse the bomb
    3
    the number is bigger then that!
    please input your combinationYou have 6 seconds left to defuse the bomb
    4
    the number is bigger then that!
    please input your combination5
    the number is bigger then that!
    please input your combinationYou have 5 seconds left to defuse the bomb
    6
    Congratulations, you defused the bomb
    $
    I did press <Enter> following each digit. (which is what I expected)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    78
    Rep Power
    2
    Hm, after some more testing, I've found that it works in the terminal as expected, but not when I run it from IDLE, which is kind of weird, but atleast I got it to work in the terminal. (It does sometimes work in IDLE though, but not all the time, which makes this even weirder)

IMN logo majestic logo threadwatch logo seochat tools logo