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

    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Unhappy Unknown Traceback?


    I'm trying to code a simple stopwatch, and I can't find out why my variables are receiving traceback errors. Everything looks fine to me... Can I have some help?

    Code:
    import time
    def stopwatch():
        def secondCheck():
            print("Please enter the seconds (TWO DIGITS!)")
            seconds = input()
            if int(seconds) > 59:
                print("That's more than 59 seconds!")
                secondCheck()
            print("0:00:" + seconds)
            minuteCheck()
        def minuteCheck():
            print("Please enter the minutes (TWO DIGITS!)")
            minutes = input()
            if int(minutes) > 59:
                print("That's more than 59 minutes!")
                minuteCheck()
            print("0:" + minutes + ":" + seconds)
            print("Please enter the hours (ONE DIGIT ONLY!")
            hourCheck()
        def hourCheck():
            hours = input()
            if int(hours) > 9:
                print("That's a two digit number, silly!")
                hourCheck()
            print(hours + ":" + minutes + ":" + seconds)
            checkIfCorrect()
        def checkIfCorrect():
            print('Is this correct? Type "yes" if it is! Otherwise, type "no".')
            answer = input()
            answer = str(answer)
            if answer == "yes":
                runStopwatch()
            if answer == "no":
                stopwatch()
        secondCheck()
    def runStopwatch():
        def changeSeconds():
            time.sleep(1)
            seconds = int(seconds)
            if seconds < 0:
                seconds = 59
                changeMinutes()
            else:
                seconds = seconds - 1
                print(hours + ":" + minutes + ":" + seconds)
        def changeMinutes():
            minutes = int(minutes)
            if minutes < 0:
                minutes = 59
                changeHours()
            else:
                minutes = minutes - 1
                print(hours + ":" + minutes + ":" + seconds)
        def changeHours():
            hours = int(hours)
            if hours > 0:
                print(hours + ":" + minutes + ":" + seconds)
                print("The timer has run out!")
                quit()
            else:
                hours = hours - 1
                print(hours + ":" + minutes + ":" + seconds)
                changeSeconds()
        changeSeconds()
    global seconds
    seconds = ""
    global minutes
    minutes = ""
    global hours
    hours = ""
    stopwatch()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    Code:
      File "test.py", line 9, in secondCheck
        print("0:00:" + seconds)
    TypeError: cannot concatenate 'str' and 'int' objects
    Is this the traceback you're getting?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by admoore
    Code:
      File "test.py", line 9, in secondCheck
        print("0:00:" + seconds)
    TypeError: cannot concatenate 'str' and 'int' objects
    Is this the traceback you're getting?
    No, I get this:
    Code:
    Traceback (most recent call last):
      File "C:/Users/ItalicGreen/Desktop/stopwatch.py", line 71, in <module>
        stopwatch()
      File "C:/Users/ItalicGreen/Desktop/stopwatch.py", line 35, in stopwatch
        secondCheck()
      File "C:/Users/ItalicGreen/Desktop/stopwatch.py", line 10, in secondCheck
        minuteCheck()
      File "C:/Users/ItalicGreen/Desktop/stopwatch.py", line 19, in minuteCheck
        hourCheck()
      File "C:/Users/ItalicGreen/Desktop/stopwatch.py", line 26, in hourCheck
        checkIfCorrect()
      File "C:/Users/ItalicGreen/Desktop/stopwatch.py", line 32, in checkIfCorrect
        runStopwatch()
      File "C:/Users/ItalicGreen/Desktop/stopwatch.py", line 64, in runStopwatch
        changeSeconds()
      File "C:/Users/ItalicGreen/Desktop/stopwatch.py", line 39, in changeSeconds
        seconds = int(seconds)
    UnboundLocalError: local variable 'seconds' referenced before assignment
    This is after I enter all the times. Once I enter "yes" it gives me these errors.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    You knew to use the global statement, good.

    bad: you put it in the wrong place.


    Put global statements at the start of functions or methods that use global variables. This version should bring you to the problem identified by admoore. gawk determines by context to use a variable as numeric or string type. awk's a wonderful language. Python is not so smart. After your input routine---you run this in python 3?????---I'm sure I saw something that made me think you'd have had a different error sooner if in python 2; the time variables have type str. 'str'+1 will fail. '09'+1 will fail.
    Code:
    import time
    def stopwatch():
        def secondCheck():
            global seconds
            print("Please enter the seconds (TWO DIGITS!)")
            seconds = input()
            if int(seconds) > 59:
                print("That's more than 59 seconds!")
                secondCheck()
            print("0:00:" + seconds)
            minuteCheck()
        def minuteCheck():
            global minutes
            print("Please enter the minutes (TWO DIGITS!)")
            minutes = input()
            if int(minutes) > 59:
                print("That's more than 59 minutes!")
                minuteCheck()
            print("0:" + minutes + ":" + seconds)
            print("Please enter the hours (ONE DIGIT ONLY!")
            hourCheck()
        def hourCheck():
            global hours
            hours = input()
            if int(hours) > 9:
                print("That's a two digit number, silly!")
                hourCheck()
            print(hours + ":" + minutes + ":" + seconds)
            checkIfCorrect()
        def checkIfCorrect():
            print('Is this correct? Type "yes" if it is! Otherwise, type "no".')
            answer = input()
            if answer == "yes":
                runStopwatch()
            if answer == "no":
                stopwatch()
        secondCheck()
    
    def runStopwatch():
        def changeSeconds():
            global seconds
            time.sleep(1)
            seconds = int(seconds)
            if seconds < 0:
                seconds = 59
                changeMinutes()
            else:
                seconds -= 1
                print(hours + ":" + minutes + ":" + seconds)
        def changeMinutes():
            global minutes
            minutes = int(minutes)
            if minutes < 0:
                minutes = 59
                changeHours()
            else:
                minutes -= 1
                print(hours + ":" + minutes + ":" + seconds)
        def changeHours():
            global hours
            hours = int(hours)
            if hours > 0:
                print(hours + ":" + minutes + ":" + seconds)
                print("The timer has run out!")
                quit()
            else:
                hours -= 1
                print(hours + ":" + minutes + ":" + seconds)
                changeSeconds()
        changeSeconds()
    
    seconds = ""
    minutes = ""
    hours = ""
    stopwatch()
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo