-3

This is my program. Everything works except the part where I need to put the bowler name, score and title (average, perfect, below average, above average). How to I make sure that all parts get into the outfile? Thanks so much!

*** OK, so I got the file output correct EXCEPT none of the titles were added. I need the output to look something like this:

Jane 160 Below Average Hector 300 PERFECT! Mary 195 Above Average Sam 210 Above Average David 102 Below Average

scores = {}  



def bowl_info(filename):
    infile = open("bowlingscores.txt", "r")
    total = 0
    for line in infile:    
        if line.strip().isdigit():
            score = int(line)    
            scores[name] = score


        else:
            name = line.strip()
    return  scores

def titles():
    for name, score in scores.items():    
        if score == 300:
            print name , score,  "PERFECT!"
        elif score < average:
            print name , score,  "Below Average"
        elif score > average:
            print name , score,  "Above Average" 
        else:
            print name , score,  "Average" 

bowl_info("bowlingscores.txt")
numbowlers = len(scores)
total = sum(scores.values())
average = total / numbowlers
titles()

for items in scores.items():    
    outfile = open("bowlingaverages.txt", "w")
Holly
  • 39
  • 1
  • 6
  • What specifically are you having trouble with? Have you tried searching for "writing to a file in python"? – Matt Coubrough Dec 03 '14 at 01:21
  • I was not sure how to make sure that the name, score and title (as defined in the function) were in the output file. I think I have everything but the title. – Holly Dec 03 '14 at 02:40

2 Answers2

2

Here is how to write to a file in python

file = open("newfile.txt", "w")

file.write("This is a test\n")

file.write("And here is another line\n")

file.close()

in your case you forget to write() and to close()

mehari
  • 3,057
  • 2
  • 22
  • 34
1

You're not actually writing to the file:

with open("bowlingaverages.txt", "w") as outfile:
    for name, score in scores.items():
        outfile.write(name + ":" + str(score))

As a side note, you should always use the with syntax when opening files, see here. This ensures that the files are close correctly no matter what. Which is something that you're not doing. Also your bowlinfo() function is not actually using it's parameter filename.

One last thing, if you are using python 2.7 then you should use scores.iteritems() instead of scores.items(). If you're using python 3 though then that's fine. See this question

EDIT

You are not getting the titles in the outfile because you are just printing them in your titles() method. You need to save the titles somewhere so you can write them to the file. Try this:

titles = {}
def titles():
    for name, score in scores.iteritems():
        if score === 300:
            titles[name] = "PERFECT!"
        elif score < average:
            titles[name] = "Below average"
        elif score > average:
            titles[name] = "Above average"
        else:
            titles[name] = "Average"

Now you have the titles saved for each player, you can change my code above to:

with open("bowlingaverages.txt", "w") as outfile:
    for name, score in scores.iteritems():
        s = name + ":" + str(score) + " " + titles[name] + "\n"
        outfile.write(s)
        # if you still want it to print to the screen as well, you can add this line
        print s

You can easily change the format of what is printed/written to the file by changing the value of s.

Community
  • 1
  • 1
Yep_It's_Me
  • 4,494
  • 4
  • 43
  • 66
  • 1
    I got this error: TypeError: expected a character buffer object – Holly Dec 03 '14 at 01:26
  • Thanks so much!! About the parameter of bowl_info...I should have either left it blank or defined filename? – Holly Dec 03 '14 at 02:39
  • It would be better to use 'with open(filename, 'r') as infile:` instead of your `open('bowlingscores.txt', 'r')` line. It's neater if you don't hardcode things like filenames into functions but pass them in as parameters. – Yep_It's_Me Dec 03 '14 at 03:33
  • Thanks!! Could you help me with one more thing?? The titles are not printing to the file. I have no idea how to include them in the outfile. I get the correct info in the python shell but the outfile is missing the title. – Holly Dec 03 '14 at 03:44