Question: Question 1 Create a function named feet_to_inches(ft) 1.1 Add assert to check 'ft' is an int with error message = 'feet is not an integer'
Question 1
- Create a function named feet_to_inches(ft)
- 1.1 Add assert to check 'ft' is an int with error message = 'feet is not an integer'
- 1.2 Convert 'ft' to 'inches'
- 1.3 Return the value of 'inches'
- Call feet_to_inches(ft) with ft = 10
- Call feet_to_inches(ft) with ft = 5
- Call feet_to_inches(ft) with ft = 0
- Call feet_to_inches(ft) with ft = 'ten'
In [ ]:
def feet_to_inches(ft)
Question 2
- Use the function named avg_grades(grades) provided
- 1.1 Add assert to check 'grade' is greater than or equal to 0 with error message = 'invalid grade - must be greater than or equal to 0'
- Define grades = [80, 0, 90, 100, 70]
- Print the result of avg_grades(grades)
In [ ]:
def avg_grades(grades):
total = 0
for grade in grades:
# INSERT YOUR ASSERT STATEMENT HERE
total += grade
return total / len(grades)
# INSERT YOUR CODE HERE
Python Testing using unittest
IMPORTANT: To Run unittest in Jupyter Notebook you have to modify unittest.main()
See https://medium.com/@vladbezden/using-python-unittest-in-ipython-or-jupyter-732448724e31
Question 3
- Use the function named lbs_to_stones (code provided)
- Use unittest to test this funtion (requires 'import' statement)
- Define a class Tests(unittest.TestCase)
- Define a test method named 'test_lbs_to_stones(self)'
- Use assertEqual to verify lbs_to_stones(140) is equal to 10
- Add if name == 'main' ... lines (see IMPORTANT note below for syntax)
# IMPORTANT: To run unittest in Jupyter you must make this change:
# if __name__ == '__main__':
# unittest.main()
#
# ... TO ...
# if __name__ == '__main__':
# unittest.main(argv=['first-arg-is-ignored'], exit=False)
#
"""
Function that need to be unit tested
"""
def lbs_to_stones(lbs):
return lbs/14
# INSERT YOUR CODE HERE
Python Debugging with Print()
Using simple print() commands is often all that's needed when debugging a problem
Question 4
Using print() statements, determine the location of the problem in this function
- Insert at least 1 print("DEBUG: ...") statement in the code provided for debugging purposes
- Use the information provided to debug and correct the code.
NOTE: LEAVE THE PRINT STATEMENTS IN THE CODE EVEN AFTER YOU CORRECT THE CODE
You must include the text string "DEBUG:" in your debugging print statements
Answers should be:
- student1: 89.60
- student2: 90.00
- student3: 76.60
- Class average: 85.40
# INSERT PRINT() STATEMENTS IN THE CODE BELOW
"""
Purpose:
1. Calculate the average grade for each student
2. Calculate the avgeage grade for the class (3 students)
"""
student_grades = {
"student1": [80, 82, 91, 95, 100],
"student2": [85, 85, 95, 95, 90],
"student3": [75, 68, 82, 78, 80] }
student_count = 0
student_total = 0
student_avg = 0
class_total = 0
class_avg = 0
for student, grades in student_grades.items():
for grade in grades:
student_total += grade
# Calculate student average
student_avg = student_total / len(grades)
# Display each student's name and average
print(f'{student}: {student_avg:.2f}')
student_count += 1
# Add student total to class total
class_total += student_total
# Calculate class average
class_avg = class_total / student_count
# Display class average
print(f'Class average: {class_avg:.2f}')
---------------------------------------------------------------------------------------------------------------------------
student1: 89.60
student2: 179.60
student3: 256.20
Class average: 427.00
Question 5
Describe at least 2 problems you found & fixed in the code provided in Question 4 (above)
Python Debugging with Logging
Logging can be used to trace the flow of your code and to flag events using different severity levels:
- logging.info()
- logging.debug()
- logging.warning()
- logging.error()
- logging.exception()
- logging.critical()
Question 6
Try some simple logging to the console
- Create a DEBUG logger message: 'This is a DEBUG message. Is this the code bug?'
- Create an INFO logger message: 'This is an INFO message. This is CSC 221 Lab2.'
- Create a WARNING logger message: 'This is a WARNING message. Be careful.'
- Create an ERROR logger message: 'This is an ERROR message. Houston - we have a PROBLEM!'
- Create a CRITICAL logger message: 'This is a CRITICAL message. OMG - Hit the brakes!!!'
REFERENCE: https://docs.python.org/3/howto/logging.html
Generate a variety of logging messages
"""
import logging
logger = logging.getLogger('Lab2_ConsoleLog')
logger.setLevel(logging.ERROR)
# INSERT YOUR CODE HERE
Question 7
Why do we only see the ERROR & CRITICAL logging messages in Question 6?
Question 8
Try some simple logging to the console and a file
Use the sample code provided at the REFERENCE URL provided as a guide
- Set the logging level to logging.WARNING
- Set the filename to 'CSC221Lab2.log'
- Set LOGGER_NAME to 'Lab2_FileLogger'
- Create a DEBUG logger message: 'This is a DEBUG message. Is this the code bug?'
- Create an INFO logger message: 'This is an INFO message. This is CSC 221 Lab2.'
- Create a WARNING logger message: 'This is a WARNING message. Be careful.'
- Create an ERROR logger message: 'This is an ERROR message. Houston - we have a PROBLEM!'
- Create a CRITICAL logger message: 'This is a CRITICAL message. OMG - Hit the brakes!!!'
REFERENCE: https://gist.github.com/wassname/d17325f36c36fa663dd7de3c09a55e74
Generate a variety of logging messages to the console and a log file
"""
import logging
import sys
# INSERT YOUR CODE HERE
Question 9
How many of the message levels go to the console & file in Question 8?
Which message levels are displayed?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
