Jun-11-2020, 02:13 PM
Use a try...except block to provide alternative code when the error arises.
from ast import literal_eval with open("C:\Dave\data.txt") as f : for line in f : name, values = line.split(' = ') values = literal_eval(values) print(f" District: {name}") try: print(f"Hal received {values[3]} votes AVG: - {values[3]/sum(values):.2%}).") except ZeroDivisionError: ...