Optional Learning Partner (OLP)
Open IDLE, File, New File, Save As tcontrol1
Have a look at the program code below and answer the questions below.
Vocabulary A function parameter is the variable listed inside the round brackets ( ) in the function. An argument is the value that is sent to the function when it is called.
import time
from time import sleep
from tkinter import *
tk=Tk()
win=Canvas(tk, width=55, height=200)
win.pack()
#functions
def red(a): #(a) is a function parameter
for i in range(a):
red=win.create_oval(5,5,50,50, fill="red")
tk.update()
time.sleep(0.05)
def redb(a):
for i in range(a):
red=win.create_oval(5,5,50,50, fill="black")
tk.update()
time.sleep(0.05)
def amber(a):
for i in range(a):
amber=win.create_oval(5,55,50,100, fill="orange")
tk.update()
time.sleep(0.05)
def amberb(a):
for i in range(a):
amber=win.create_oval(5,55,50,100, fill="black")
tk.update()
time.sleep(0.05)
def green(a):
for i in range(a):
green=win.create_oval(5,105,50,150, fill="green")
tk.update()
time.sleep(0.05)
def greenb(a):
for i in range(a):
green=win.create_oval(5,105,50,150, fill="black")
tk.update()
time.sleep(0.05)
def lights():
red=win.create_oval(5,5,50,50, fill="black")
amber=win.create_oval(5,55,50,100, fill ="black")
green=win.create_oval(5,105,50,150, fill="black")`
#end of functions
#calling the functions
lights()
red(30) #(30) is the argument sent to the red function
redb(1)
amber(10)
amberb(1)
green(30)
greenb(1)
tk.mainloop()
Predict (OLP)
1. Look and describe what you think it will do when run. Do not copy code in your answer.
Run (OLP)
2, Now run the code to see if you were right
Investigate (OLP)
3, How many functions are there?
4, What does the parameter (a) do in most functions?
5, Name the three Python libraries used in this program
Modify (OLP)
6, Change the code to make the green light stay on for double the amount of time. What did you change?
7. Change the code to reduce the height of the window so there is no extra space below the lights. What did you change?
Now mark your work before making.
Make (Work on your own)
8, Save your work as tcontrol2. Extend the code to make the traffic lights change back up green, amber, red.
9, Save your work as tcontrol3. Extend the code to make the traffic lights sequence loop round 10 times.
10, Save your work as tcontrol4. Change the code to make the traffic light loop round forever.
HINT While