[Tutor] reading complex data types from text file
Chris Castillo
ctcast at gmail.com
Sun Jul 19 07:15:16 CEST 2009
so could I also replace the score of each bowler (key value) in the
dictionary with a new key such as "below average" or "above average"
according to each if-elif-else structure and then write to a text file in
the following format?
Jim Above Average
Sue Below Average
Bob Perfect score
On Fri, Jul 17, 2009 at 12:48 PM, bob gailer <bgailer at gmail.com> wrote:
> Chris Castillo wrote:
>
>> how would i go about adding the names to a dictionary as a key and the
>> scores as a value in this code?
>>
>>
>> # refactored for better use of Python, correct logic, and flow
>
> scores = {} # empty dictionary
> total = 0
> for line in open("bowlingscores.txt", "r"):
> if line.strip().isdigit():
> score = int(line)
> scores[name] = score
> total += score
> else:
> name = line.strip()
> averageScore = total / len(scores)
> fileOut = open("bowlingaverages.txt", "w")
> fileOut.write("Bowling Report\n" + ("-" * 50) + "\n")
> for name, score in scores.items():
> if score == 300:
> score = "\tPerfect score!"
> elif score < averageScore:
> score = "\tBelow average"
> elif score > averageScore:
> score = "\tAbove average!"
> else:
> score = "\tAverage!"
> print name, score
>
>
> --
> Bob Gailer
> Chapel Hill NC
> 919-636-4239
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090719/36e9765a/attachment.htm>
More information about the Tutor
mailing list