Search code examples
pythonfunctionreturnreturn-value

Python function not returning values


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

Solution

  • 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)