By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,001 Members | 1,262 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,001 IT Pros & Developers. It's quick & easy.

How do I strip a new line?

P: 23
It only catches the comparison to average for the first name and i need to strip the new line.
Can you help?


Expand|Select|Wrap|Line Numbers
  1. scores = []
  2. names = []
  3. infile = open("bowlingscores.txt", "r")
  4. for i, line in enumerate(infile):
  5.     if i % 2 == 1:
  6.         scores.append(int(line))
  7.     else:
  8.         names.append(line)
  9. average = float(sum(scores) / len(scores))
  10. print (names)
  11. print (scores)
  12. outfile = open("bowlingaverages.txt", "w")
  13. for s in zip(names, scores):
  14.     if scores > average:
  15.         scores = "above average"
  16.     elif scores < average:
  17.         scores = "below average"
  18.     else:
  19.         scores = "perfect"
  20. outfile.write("%s" % names)
  21. outfile.write("%s" % scores)
  22.  
  23.  
Nov 17 '10 #1
Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
String method strip() strips leading and trailing whitespace such as newline characters.
Expand|Select|Wrap|Line Numbers
  1. >>> 
  2. >>> line = "123456\n"
  3. >>> line.strip()
  4. '123456'
  5. >>> 
Nov 17 '10 #2

P: 23
Where in my code would it go?
Also how do I make it go through my loop every time? It is only going through once.
Nov 18 '10 #3

P: 23
Here is how i changed it:
Expand|Select|Wrap|Line Numbers
  1. scores = []
  2. names = []
  3. averagescore = []
  4. infile = open("bowlingscores.txt", "r")
  5. for i, line in enumerate(infile):
  6.     if i % 2 == 1:
  7.         scores.append(int(line))
  8.     else:
  9.         names.append(line)
  10. average = float(sum(scores) / len(scores))
  11. print(average)
  12. print (names)
  13. print (scores)
  14. outfile = open("averages.txt", "w")
  15. scores = str(scores)
  16. for score in (scores):
  17.     if score > average:
  18.         averagescore.append("above average")
  19.     elif score < average:
  20.         averagescore.append("below average")
  21.     else:
  22.         averagescore.append("perfect")
  23. for s in zip(names, averagescore):
  24.     outfile.write(s)
  25.  
It keeps giving me this error on the line that reads outfile.write(s):
TypeError: argument 1 must be a string or read only character buffer, not tuple
Nov 18 '10 #4

bvdet
Expert Mod 2.5K+
P: 2,851
It should be:
Expand|Select|Wrap|Line Numbers
  1. for tup in zip(names, averagescore):
  2.     outfile.write(" ".join(tup))
Nov 18 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.