-2

when reading the file it is coming up as "None", how can i fix this so that it is a dictionary(made from two varibles) in a file.

with open('RPS.txt','r') as f:
    highscores = [line.strip() for line in f]
    f = open('RPS.txt','a')
    high = {player_score: name}
    highscores = highscores.append(high)
    print(highscores, file=f)
    f.close()
  • 1
    you cannot open the same file twice – gdlmx May 14 '16 at 23:16
  • i fixed it with f.close() after the second line... still have same problem – A.Smith May 14 '16 at 23:20
  • @gdlmx - sure you can. It might not do what you want but in this case if OP just wants to read and append the file, it will work. – tdelaney May 14 '16 at 23:21
  • There are some basic code smells here: opening the same file twice, using the same variable name both times the file is opened, creating the high & highscores objects inside the routine that outputs them. – BM5k May 14 '16 at 23:24
  • What is `None`? Nothing is `None` in this script. Please show us the exact error. And please make this a runnable example - not a zillion lines of code, just enough to demonstrate. You never write the file so what's the point in reopening it in append mode? – tdelaney May 14 '16 at 23:24
  • Open the file with 'rw' permission once, fix the `highscores=...` error – gdlmx May 14 '16 at 23:25
  • Also: if you've updated the code (i.e. added `f.close()` after the second line) please update your question with the new code. – BM5k May 14 '16 at 23:25
  • tdelaney, when i print the file it literally comes up as "None" in the shell – A.Smith May 14 '16 at 23:28
  • @gdlmx that mode works for python 2 but not 3. `"a+"` is another option. – tdelaney May 14 '16 at 23:28

1 Answers1

3
highscores = highscores.append(high)

This set highscores to None. append returns None, do not set it to your variable.

romain-aga
  • 1,441
  • 9
  • 14