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
2 Answers 2
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.
4 Comments
ShotTime(matchtime, shottime) in countdown with matchtime, shottime = ShotTime(matchtime, shottime)"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
Explore related questions
See similar questions with these tags.
conditiondon't match both the cases ?. So try putting thereturnout side theif..elseToplevel. Please create an example that can be run exactly as written and show both the actual output and what you expected.