I'm writing a Dice Game in Python and at the end of the program it writes the top 5 scores on separate lines, with the player's name in an external file. I've written this code in a separate file for testing but I get the following error when I run it:
filename.write (var)
TypeError: must be str, not float
This is my source code for the file handling.
L = [29.4, 29.2, 1.4, 2.9, 2.3]
print (L)
x = len (L)
filename = open ('Scores.txt','rt')
filename.close
if x == 5:
for i in range (0,5):
var = (L[i])
print(var)
filename.write (var)
filename.close
Would greatly appreciate if someone could help :)
filename.closedo absolutely nothing (because you're missing the()), and you probably don't want to close the file right after you've opened it anyway.filename.write(str(var)). Also, why are you opening the file, closing the file, and then closing the file again?