Python bowling program dictionary from text file -
below assignment. stuck on how include total in dictionary. not sure if possible need average. appreciate push in right direction. :)
assignment:write program read unknown number of bowlers , bowling scores (with possible values 1 300) external file called "bowlingscores.txt". file similar following:
david 102 hector 300 mary 195 jane 160 sam 210
output bowlers’ names external data file called "bowlingaverages.txt". next each bowler's name, print message dependent on scores: perfect scores (equal 300), output “perfect” scores greater average score, output “above average” below average, output “below average”
scores = {} total = 0 def bowl_info(filename): infile = open("bowlingscores.txt", "r") line in infile: if line.strip().isdigit(): score = int(line) scores[name] = score total += score else: name = line.strip() return scores bowl_info("bowlingscores.txt") numbowlers = len(scores) total = 0 average = total / numbowlers
would not possible add total key in dictionary , update in loop have done?
scores = {'total': 0} def bowl_info(filename): infile = open("bowlingscores.txt", "r") line in infile: if line.strip().isdigit(): score = int(line) scores[name] = score scores['total'] += score else: name = line.strip() return scores bowl_info("bowlingscores.txt") numbowlers = len(scores) #total = 0 remove line average = scores['total'] / numbowlers
Comments
Post a Comment