0

I'm creating a digital clock timer with two timers. The first is 30 mins and second is 30 to 20 secs depending on how long is left in the first timer. To reset the second clock every 30 or 20 seconds i created a function to call it to set the shottimer back to 30. However it is not returning the value of the shot timer any ideas why. Code is below

def countdown(matchtime,shottime):
    matchstr = str(datetime.timedelta(seconds=matchtime))
    shottimestr = str(datetime.timedelta(seconds=shottime))
    lbl_text['text'] = matchstr
    lbl_textshot['text'] = shottimestr
    if shottime == 0:

        ShotTime(matchtime, shottime)
        print (shottime)
    if matchtime > 0:
        root.after(1000, countdown, matchtime-1, shottime-1)    
        print (shottime)    
        matchstr = str(datetime.timedelta(seconds=matchtime))
        shottimestr = str(datetime.timedelta(seconds=shottime))

        lbl_text['text'] = matchstr
        lbl_textshot['text'] = shottimestr


    elif(matchtime == 0):
        global NewForm
        NewForm = Toplevel() 
        NewForm.title("Sourcecodester")
        width = 500
        height = 300
        screen_width = root.winfo_screenwidth()
        screen_height = root.winfo_screenheight()
        x = (screen_width/2) - (width/2)
        y = (screen_height/2) - (height/2)
        NewForm.geometry("%dx%d+%d+%d" % (width, height, x, y))
        NewForm.resizable(0, 0)
        lbl_blast = Label(NewForm, text="Blast Off!", font=('arial', 50))
        lbl_blast.pack(fill=BOTH, pady=100)
        btn_back = Button(NewForm, text="Reset", font=('arial', 16), command=BackBtn)
        btn_back.pack(side=TOP)   
def ShotTime(matchtime, shottime):
        if shottime == 0 and matchtime > 900:
            shottime = 30
            return matchtime, shottime
        elif matchtime <= 900 and shottime == 0:
            shottime = 20
            return matchtime, shottime
9
  • What if the condition don't match both the cases ?. So try putting the return out side the if..else Commented Mar 1, 2019 at 17:58
  • Please fix your indentation. It's impossible to tell where the error is when the second line crashes the interpreter :) Commented Mar 1, 2019 at 17:58
  • 1
    Is the indentation a formating error on when you posted your code, or is it like this in your code. I assuming it is just in SO, otherwise you would get an error. Would you be able to fix it? Commented Mar 1, 2019 at 17:58
  • This code won't run as is. In addition to indentation issues with the first function, there are several undefined terms like Toplevel. Please create an example that can be run exactly as written and show both the actual output and what you expected. Commented Mar 1, 2019 at 17:59
  • 1
    Fix the indentation on the post Commented Mar 1, 2019 at 18:09

2 Answers 2

1

You have a return statement in def ShotTime but you don't have ShotTime equal to anything.

Edit: To elaborate more you have `def ShotTime(matchtime, shottime):

if shottime == 0 and matchtime > 900:
    shottime = 30
    return matchtime, shottime
elif matchtime <= 900 and shottime == 0:
    shottime = 20
    return matchtime, shottime`

So you have return statements there.

if shottime == 0:

ShotTime(matchtime, shottime)
print (shottime)

but in def countdown() you dont have it being set equal to anything. I believe in python you have to do something like x = ShotTime(matchtime,shottime) and that will return a array, then do like matchtime = x[0] then shottime = x[1]

edit2: this is a better way thx @kevin matchtime, shottime = ShotTime(matchtime, shottime)

This has to do with variable scope. Unless it is a global variable, variables stay in the function they are created. Just because it has the same name does not mean it is the same variable.

Sign up to request clarification or add additional context in comments.

4 Comments

huh? I'm not sure what that means
ShotTime is the function shottime,matchtime are the value that should be return
I interpret this to mean "returning a value from ShotTime isn't useful unless you assign the result to something. Replace ShotTime(matchtime, shottime) in countdown with matchtime, shottime = ShotTime(matchtime, shottime)"
Thanks u this works im not too fimlair with python i though when i return shottime and matchtime from def Shottime it would automatically change the values as it uses them values. thanks again
0

The function ShotTime(matchtime, shottime) takes its parameters by value not by reference. Setting

shottime = 30

Will only affect the value you return. You're not using that value. e.g.

ShotTime(matchtime, shottime)

You may wish to change to

matchtime, shottime = ShotTime(matchtime, shottime)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.