Practice Debugging
Many of you will finish these exercises before the end of class. Use the extra time to work on your current assignment.
If and when you have questions outside of class, we strongly encourage you to use the email address comp200@. You'll likely get a quicker response than if you email one of the instructors, since this is read by all the course staff.
Exercises
Each of the following mini-programs are broken. Some have multiple mistakes. Some errors lead to error messages, but some only lead to the wrong results. Their documentation strings describe what they should do. Fix them.
-
def is_square(num): """Returns whether the given number is square, e.g., 0, 1, 4, 9.""" return num = int(math.sqrt(nun)) ** 2
-
def area_circle(radius): """Returns the area of a circle of the given radius.""" return pi * radius ** 2
-
def max2(num1, num2): """Returns the larger of the two inputs.""" if num1 > num2 return num1 else return num2 def max3(num1, num2, num3): """Returns the largest of the three inputs.""" return max2(max_2(num1, num2), (num3)
-
def weather_forecast(city): """Returns an overly simple forecast for the given city.""" return 'It's a beautiful day in' + city + '.'
-
count = 0 def count_instructor(person): """Increases count_instructor by one each time the input person is 'John' or 'Stephen'.""" if person == "John" or "Stephen": count_instructor += 1 return count # returns the count value as convenience to make testing easier print 'count_instructor("John") = ', count_instructor("John") # return value should be 1 print 'count = ', count # count should be 1 print 'count_instructor("Mary") = ', count_instructor("Mary") # count and return value should be 1 print 'count = ', count # count should be 1 print 'count_instructor("John") = ', count_instructor("John") # return value should be 2 print 'count = ', count # count should be 2 print 'count_instructor("Stephen") = ', count_instructor("Stephen") # return value should be 3 print 'count = ', count # count should be 3 print 'count_instructor("Fred") = ', count_instructor("Fred") # return value should be 3 print 'count = ', count # count should be 3 #################################################################### ###### The following functions are for testing purposes only ##### ###### Include them in your code but do not edit them! ############# def reset(): """ Used for testing -- DO NOT EDIT! Resets the count to zero. """ global count count = 0 def get_count(): """ Used for testing -- DO NOT EDIT! Returns the count value """ global count return count