Appendix F — Practice Problem Solutions — Chapter 6
F.1 Solution Section 6.9.1
try:
value = float("abc")
except ValueError:
print("Not a valid number")F.2 Solution Section 6.9.2
counts = {"A": 1, "C": 2, "G": 0, "T": 4}
total = sum(counts.values())
try:
n_ratio = counts["N"] / total
except KeyError:
print("N is not present in the counts dictionary")
n_ratio = NoneF.3 Solution Section 6.9.3
try:
silly_divide(5, 0)
except ZeroDivisionError:
print("you can't divide by zero!")
except Exception as error:
print(f"a mysterious error occurred: {error=}")F.4 Solution Section 6.9.4
def fold_change(expression_1, expression_2)
try:
return expression_1 / expression_2
except ZeroDivisionError:
print("expression_2 was zero!")
return NoneF.5 Solution Section 6.9.5
class SequenceLengthError(Exception):
pass
MIN_LENGTH = 50
MAX_LENGTH = 150
def validate_sequence_length(sequence):
sequence_length = len(sequence)
if sequence_length < MIN_LENGTH:
raise SequenceLengthError(f"sequence length {sequence_length} was too short!")
if sequence_length > MAX_LENGTH:
raise SequenceLengthError(f"sequence length {sequence_length} was too long!")
return NoneF.6 Solution Section 6.9.6
def run_simulation(max_turns):
if max_turns < 1:
raise ValueError(f"Expected at least 1 iteration, but got {max_turns=}")
if max_turns > 1000:
raise ValueError(f"Expected at most 1000 iterations, but got {max_turns=}")
# Simulation code would follow
pass