forked from CodeMouse92/DeadSimplePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraffic_generator_throw.py
More file actions
40 lines (32 loc) · 932 Bytes
/
traffic_generator_throw.py
File metadata and controls
40 lines (32 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from random import choice
colors = ['red', 'green', 'blue', 'silver', 'white', 'black']
vehicles = ['car', 'truck', 'semi', 'motorcycle', None]
def traffic():
while True:
vehicle = choice(vehicles)
color = choice(colors)
try:
yield f"{color} {vehicle}"
except ValueError:
print(f"Skipping {color} {vehicle}...")
continue
except GeneratorExit:
print("No more vehicles.")
raise
def wash_vehicle(vehicle):
if 'semi' in vehicle:
raise ValueError("Cannot wash vehicle.")
print(f"Washing {vehicle}.")
def car_wash(traffic, limit):
count = 0
for vehicle in traffic:
try:
wash_vehicle(vehicle)
except Exception as e:
traffic.throw(e)
else:
count += 1
if count >= limit:
traffic.close()
queue = traffic()
car_wash(queue, 10)