okay so I figured my program out. I am posting the final version so if
someone else is having problems with something like this it may benefit the
community. (comments are included to help those who might not understand the
code)
------------------------------------------------------------------------------------------------------------
'''
Program reads names of bowlers and their corresponding scores,
then writes their names to a new text file with a description
of either below average, average, above average or a perfect score
'''

scores = {}    # empty dictionary
total = 0    #initalize total to 0 value


for line in open("bowlingscores.txt", "r"):    # iterate through txt file
with names and scores
    if line.strip().isdigit():
        score = int(line)    # convert score into int type
        scores[name] = score    # add scores to dictionary
        total += score    # add score to running total
    else:
        name = line.strip()    # if the line isn't a digit name will be the
key in the dictionary



averageScore = total / len(scores)    # get average of all scores
fileOut = open("bowlingaverages.txt", "w")    # create a file to write names
and scores to
fileOut.write("Bowling Report\n" + ("-" * 50) + "\n")    # header


for name, score in scores.items():    #iterate through each score in the
dictionary to get an score value for each player
    if score == 300:
        score = "\tPerfect score!\n"
        scores[name] = score
    elif score < averageScore:
        score = "\tBelow average\n"
        scores[name] = score
    elif score > averageScore:
        score = "\tAbove average!\n"
        scores[name] = score
    else:
        score = "\tAverage!\n"
        scores[name] = score

for items in scores.items():    #iterate through the items in the dictionary
and format them to the output file
    fileOut.write("%s%s\n" % items)
---------------------------------------------------------------------------------------
your output for this code should look like this inside the text file:

*Bowling Report
--------------------------------------------------
sue    Below average

bill    Above average!

nat    Below average

tom    Perfect score!*



Thanks to everyone who helped me with this.

On Sun, Jul 19, 2009 at 12:15 AM, Chris Castillo <ctc...@gmail.com> wrote:

> 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 <bgai...@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
>>
>
>
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to