Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6dc1093
commit dbc9e32
Showing
2 changed files
with
29 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!python3 | ||
#traffic light using itertools and random modules | ||
|
||
from time import sleep | ||
import itertools | ||
import random | ||
|
||
colours = 'Red Green Amber'.split() | ||
rotation = itertools.cycle(colours) | ||
|
||
def rg_timer(): | ||
return random.randint(5,9) | ||
|
||
def light_rotation(rotation): | ||
for colour in rotation: | ||
if colour == 'Amber': | ||
print('Caution! The light is %s.\n' % colour) | ||
sleep(5) | ||
elif colour == 'Red': | ||
print('Stop! The light is %s.\n' % colour) | ||
sleep(rg_timer()) | ||
else: | ||
print('Go! The light is %s.\n' % colour) | ||
sleep(rg_timer()) | ||
|
||
|
||
if __name__ == '__main__': | ||
light_rotation(rotation) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters