6.0001 | Fall 2016 | Undergraduate

# Introduction to Computer Science and Programming in Python

In-Class Questions and Video Solutions

## Lecture 7

1. #### Black Box and Glass Box Testing

With the below implementation, is the test set “n = 4 | n = -4 | n = 5” path complete?

``````def is_even(n):
"""
Returns True if a number is even
and False if not
"""
if n > 0 and n % 2 == 0:
return True
elif n < 0 and n % 2 == 0:
return True
else:
return False
``````

check
close

With the above implementation, which value for n is incorrectly labeled by is_even?

close
close
check

1. #### Errors

Below is a piece of code and an error shown when running it. What is the problem?

``````L = 3
for i in range(len(L)):
print(i)

ERROR MESSAGE:

File "C:/Users/Ana/.spyder2-py3/temp.py", line 2, in
for i in range(len(L)):

TypeError: object of type 'int' has no len()
``````

close
close
check
close

2. #### Exceptions

If the user enters “twenty” in the code below what does the program do?

``````try:
n = int(input("How old are you? "))
percent = round(n*100/80, 1)
print("You've gone through", percent, "% of your life!")
except ValueError:
print("Oops, must enter a number.")
except ZeroDivisionError:
print("Division by zero.")
except:
print("Something went very wrong.")
``````

close
check

If the user enters “0” in the code above what does the program do?

check
close

## Course Info

Fall 2016
##### Learning Resource Types
Problem Sets
Lecture Notes
Lecture Videos
Programming Assignments with Examples