def list_max(l): max_val = l[0] for val in l: if val > max_val: max_val = val return max_val def list_max_better(l): if l == []: raise Exception("list must be non-empty") max_val = l[0] for val in l: if val > max_val: max_val = val return max_val def get_scores(): print("Enter the scores one at a time. Blank score finishes.") scores = [] line = input("Enter score: ") while line != "": scores.append(float(line)) line = input("Enter score: ") return scores def get_scores_better(): print("Enter the scores one at a time. Blank score finishes.") scores = [] line = input("Enter score: ") while line != "": try: scores.append(float(line)) except ValueError: print("Enter numbers only!") line = input("Enter score: ") return scores def print_file_stats(filename): file = open(filename, "r") longest = "" shortest = "" total_length = 0 count = 0 for word in file: word = word.strip() if len(word) > len(longest): longest = word if shortest == "" or len(word) < len(shortest): shortest = word total_length += len(word) count += 1 file.close() print("Number of words: " + str(count)) print("Longest word: " + longest) print("Shortest word: " + shortest) print("Avg. word length: " + str(float(total_length)/count)) def print_file_stats_better(filename): try: print_file_stats(filename) except FileNotFoundError: print("File doesn't exist. No stats :(") except ZeroDivisionError: pass