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)<br>
------------------------------------------------------------------------------------------------------------<br>'''<br>Program reads names of bowlers and their corresponding scores,<br>then writes their names to a new text file with a description<br>
of either below average, average, above average or a perfect score<br>'''<br><br>scores = {} # empty dictionary<br>total = 0 #initalize total to 0 value<br><br><br>for line in open("bowlingscores.txt", "r"): # iterate through txt file with names and scores<br>
if line.strip().isdigit():<br> score = int(line) # convert score into int type<br> scores[name] = score # add scores to dictionary<br> total += score # add score to running total<br> else:<br>
name = line.strip() # if the line isn't a digit name will be the key in the dictionary<br><br> <br> <br>averageScore = total / len(scores) # get average of all scores<br>fileOut = open("bowlingaverages.txt", "w") # create a file to write names and scores to<br>
fileOut.write("Bowling Report\n" + ("-" * 50) + "\n") # header<br><br><br>for name, score in scores.items(): #iterate through each score in the dictionary to get an score value for each player<br>
if score == 300:<br> score = "\tPerfect score!\n"<br> scores[name] = score <br> elif score < averageScore:<br> score = "\tBelow average\n"<br> scores[name] = score<br>
elif score > averageScore:<br> score = "\tAbove average!\n"<br> scores[name] = score<br> else:<br> score = "\tAverage!\n"<br> scores[name] = score<br><br>for items in scores.items(): #iterate through the items in the dictionary and format them to the output file<br>
fileOut.write("%s%s\n" % items)<br>---------------------------------------------------------------------------------------<br>your output for this code should look like this inside the text file:<br><br><b>Bowling Report<br>
--------------------------------------------------<br>sue Below average<br><br>bill Above average!<br><br>nat Below average<br><br>tom Perfect score!</b><br><br><br><br>Thanks to everyone who helped me with this.<br>
<br><div class="gmail_quote">On Sun, Jul 19, 2009 at 12:15 AM, Chris Castillo <span dir="ltr"><<a href="mailto:ctcast@gmail.com">ctcast@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
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?<br>
<br>Jim Above Average<br>Sue Below Average<br>Bob Perfect score<div><div></div><div class="h5"><br><br><div class="gmail_quote">On Fri, Jul 17, 2009 at 12:48 PM, bob gailer <span dir="ltr"><<a href="mailto:bgailer@gmail.com" target="_blank">bgailer@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div>Chris Castillo wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
how would i go about adding the names to a dictionary as a key and the scores as a value in this code?<br>
<br>
<br>
</blockquote></div>
# refactored for better use of Python, correct logic, and flow<br>
<br>
scores = {} # empty dictionary<br>
total = 0<br>
for line in open("bowlingscores.txt", "r"):<br>
if line.strip().isdigit():<br>
score = int(line)<br>
scores[name] = score<br>
total += score<br>
else:<br>
name = line.strip()<br>
averageScore = total / len(scores)<div><br>
fileOut = open("bowlingaverages.txt", "w")<br>
fileOut.write("Bowling Report\n" + ("-" * 50) + "\n")<br></div>
for name, score in scores.items():<div><br>
if score == 300:<br>
score = "\tPerfect score!"<br></div>
elif score < averageScore:<br>
score = "\tBelow average"<br>
elif score > averageScore:<br>
score = "\tAbove average!"<br>
else:<br>
score = "\tAverage!"<br>
print name, score<div><div></div><div><br>
<br>
-- <br>
Bob Gailer<br>
Chapel Hill NC<br>
919-636-4239<br>
</div></div></blockquote></div><br>
</div></div></blockquote></div><br>