Skip to content

Commit

Permalink
bite 102submit and 101bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
boraxpr committed Oct 22, 2019
1 parent ec7515a commit ae0a34e
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
4 changes: 2 additions & 2 deletions 101/driving.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
def allowed_driving(name, age):
"""Print '{name} is allowed to drive' or '{name} is not allowed to drive'
checking the passed in age against the MIN_DRIVING_AGE constant"""
if age > MIN_DRIVING_AGE:
if age >= MIN_DRIVING_AGE:
print(name + " is allowed to drive")
else:
print(name + " is not allowed to drive")

allowed_driving("Tim", 29)
# allowed_driving("Tim", 29)
28 changes: 28 additions & 0 deletions 102/colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
VALID_COLORS = ['blue', 'yellow', 'red']


def print_colors():
"""In the while loop ask the user to enter a color,
lowercase it and store it in a variable. Next check:
- if 'quit' was entered for color, print 'bye' and break.
- if the color is not in VALID_COLORS, print 'Not a valid color' and continue.
- otherwise print the color in lower case."""
while True:
print("A Color: ")
inp = input()
input_lowered = inp.lower()
valid = 0
if input_lowered == "quit":
print("bye")
break
for color in VALID_COLORS:
if color == input_lowered:
valid = 1
if valid == 1:
print(input_lowered)
else:
print("Not a valid color")
continue
pass

print_colors()
63 changes: 63 additions & 0 deletions 102/test_colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from unittest.mock import patch

from colors import print_colors

NOT_VALID = 'Not a valid color'


def call_print_colors():
# some people prefer sys.exit instead of break
try:
print_colors()
except SystemExit:
pass


@patch("builtins.input", side_effect=['quit'])
def test_straight_quit(input_mock, capsys):
# user only enter quit, program prints bye and breaks loop
call_print_colors()
actual = capsys.readouterr()[0].strip()
expected = 'bye'
assert actual == expected


@patch("builtins.input", side_effect=['blue', 'quit'])
def test_one_valid_color_then_quit(input_mock, capsys):
# user enters blue = valid color so print it
# then user enters quit so break out of loop = end program
call_print_colors()
actual = capsys.readouterr()[0].strip()
expected = 'blue\nbye'
assert actual == expected


@patch("builtins.input", side_effect=['green', 'quit'])
def test_one_invalid_color_then_quit(input_mock, capsys):
# user enters green which is not in VALID_COLORS so continue the loop,
# user then enters quit so loop breaks (end function / program)
call_print_colors()
actual = capsys.readouterr()[0].strip()
expected = f'{NOT_VALID}\nbye'
assert actual == expected


@patch("builtins.input", side_effect=['white', 'red', 'quit'])
def test_invalid_then_valid_color_then_quit(nput_mock, capsys):
# white is not a valid color so continue the loop,
# then user enters red which is valid so print it, then quit
call_print_colors()
actual = capsys.readouterr()[0].strip()
expected = f'{NOT_VALID}\nred\nbye'
assert actual == expected


@patch("builtins.input", side_effect=['yellow', 'orange', 'quit'])
def test_valid_then_invalid_color_then_quit(input_mock, capsys):
# yellow is a valid color so print it, user then enters orange
# which is not a valid color so continue loop, lastly user
# enters quit so exit loop = reaching end function / program
call_print_colors()
actual = capsys.readouterr()[0].strip()
expected = f'yellow\n{NOT_VALID}\nbye'
assert actual == expected

0 comments on commit ae0a34e

Please sign in to comment.