Skip to content

Commit ae0a34e

Browse files
author
boraxpr
committed
bite 102submit and 101bugfix
1 parent ec7515a commit ae0a34e

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

101/driving.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
def allowed_driving(name, age):
55
"""Print '{name} is allowed to drive' or '{name} is not allowed to drive'
66
checking the passed in age against the MIN_DRIVING_AGE constant"""
7-
if age > MIN_DRIVING_AGE:
7+
if age >= MIN_DRIVING_AGE:
88
print(name + " is allowed to drive")
99
else:
1010
print(name + " is not allowed to drive")
1111

12-
allowed_driving("Tim", 29)
12+
# allowed_driving("Tim", 29)

102/colors.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
VALID_COLORS = ['blue', 'yellow', 'red']
2+
3+
4+
def print_colors():
5+
"""In the while loop ask the user to enter a color,
6+
lowercase it and store it in a variable. Next check:
7+
- if 'quit' was entered for color, print 'bye' and break.
8+
- if the color is not in VALID_COLORS, print 'Not a valid color' and continue.
9+
- otherwise print the color in lower case."""
10+
while True:
11+
print("A Color: ")
12+
inp = input()
13+
input_lowered = inp.lower()
14+
valid = 0
15+
if input_lowered == "quit":
16+
print("bye")
17+
break
18+
for color in VALID_COLORS:
19+
if color == input_lowered:
20+
valid = 1
21+
if valid == 1:
22+
print(input_lowered)
23+
else:
24+
print("Not a valid color")
25+
continue
26+
pass
27+
28+
print_colors()

102/test_colors.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from unittest.mock import patch
2+
3+
from colors import print_colors
4+
5+
NOT_VALID = 'Not a valid color'
6+
7+
8+
def call_print_colors():
9+
# some people prefer sys.exit instead of break
10+
try:
11+
print_colors()
12+
except SystemExit:
13+
pass
14+
15+
16+
@patch("builtins.input", side_effect=['quit'])
17+
def test_straight_quit(input_mock, capsys):
18+
# user only enter quit, program prints bye and breaks loop
19+
call_print_colors()
20+
actual = capsys.readouterr()[0].strip()
21+
expected = 'bye'
22+
assert actual == expected
23+
24+
25+
@patch("builtins.input", side_effect=['blue', 'quit'])
26+
def test_one_valid_color_then_quit(input_mock, capsys):
27+
# user enters blue = valid color so print it
28+
# then user enters quit so break out of loop = end program
29+
call_print_colors()
30+
actual = capsys.readouterr()[0].strip()
31+
expected = 'blue\nbye'
32+
assert actual == expected
33+
34+
35+
@patch("builtins.input", side_effect=['green', 'quit'])
36+
def test_one_invalid_color_then_quit(input_mock, capsys):
37+
# user enters green which is not in VALID_COLORS so continue the loop,
38+
# user then enters quit so loop breaks (end function / program)
39+
call_print_colors()
40+
actual = capsys.readouterr()[0].strip()
41+
expected = f'{NOT_VALID}\nbye'
42+
assert actual == expected
43+
44+
45+
@patch("builtins.input", side_effect=['white', 'red', 'quit'])
46+
def test_invalid_then_valid_color_then_quit(nput_mock, capsys):
47+
# white is not a valid color so continue the loop,
48+
# then user enters red which is valid so print it, then quit
49+
call_print_colors()
50+
actual = capsys.readouterr()[0].strip()
51+
expected = f'{NOT_VALID}\nred\nbye'
52+
assert actual == expected
53+
54+
55+
@patch("builtins.input", side_effect=['yellow', 'orange', 'quit'])
56+
def test_valid_then_invalid_color_then_quit(input_mock, capsys):
57+
# yellow is a valid color so print it, user then enters orange
58+
# which is not a valid color so continue loop, lastly user
59+
# enters quit so exit loop = reaching end function / program
60+
call_print_colors()
61+
actual = capsys.readouterr()[0].strip()
62+
expected = f'yellow\n{NOT_VALID}\nbye'
63+
assert actual == expected

0 commit comments

Comments
 (0)