There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
I am having difficulties to create a unit test for the second function-request_flight_attendant() in the monitor.py
How can I make a test that checks whether the requesting the flight attendant action creates the intended warning which is warnings.warn('A customer has requested attention!', CustomerRequestWarning)
Hi there,
Please can anyone nudge me in the right direction, I was expecting this unit test to fail on the <1 and ‘string’ iterations of the test loop, what am I missing? Thanks in advance
import unittest
import warnings
class CustomerRequestWarning(Warning):
pass
def calculate_remaining_time(km):
'''
This function estimates the remaining flight time when given the kilometers that remain for this flight. The function assumes an average speed of 15 km per minute and rounds down when calculating minutes. It returns a tuple of hours and then minutes remaining.
'''
total_minutes = int(km / 15)
minutes = total_minutes % 60
hours = int(total_minutes / 60)
return (hours, minutes)
def request_flight_attendant():
'''
This function raises a CustomerRequestWarning to let the flight attendants know that they are requested.
'''
warnings.warn('A customer has requested attention!', CustomerRequestWarning)
class SmallWorldTest(unittest.TestCase):
def test_calculate_remaining_time(self):
for num in [15, 900, 2730, 5, 0, -1, 'hey']:
with self.subTest():
if type(num) is not int:
expected_result = 'Please enter a valid distance'
elif num < 1:
expected_result = 'You have reached your destination'
else:
total_minutes = int(num / 15)
minutes = total_minutes % 60
hours = int(total_minutes / 60)
expected_result = (hours, minutes)
message = 'Expected calculate_remaining_time(' + str(num) + ') to return ' + str(expected_result)
self.assertEqual(calculate_remaining_time(num), expected_result, message)
unittest.main()