2

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 :)

2
  • FWIW, your two filename.close do absolutely nothing (because you're missing the ()), and you probably don't want to close the file right after you've opened it anyway. Commented Jul 10, 2019 at 12:26
  • Use filename.write(str(var)). Also, why are you opening the file, closing the file, and then closing the file again? Commented Jul 10, 2019 at 12:27

3 Answers 3

2

To answer your question, and also clean up your code some....

You can convert the item to a string by using the str() operator.

filename.write(str(var))

Also, it is considered standard and safer to use the with command to open a file. It handles the exceptions for you, and automagically closes the file.

Furthermore, iterating through the indices of a list is considered non-Pythonic. It's better to do for item in list, which gives you the item without needing to do a list access (list[i]).

So, in the end, a cleaner version would look like this:

L = [29.4, 29.2, 1.4, 2.9, 2.3]
print(L)
with open('Scores.txt','w') as filename:
    for item in L[:5]: #For each item in the first 5 items
        print(item)
        filename.write(str(item))
Sign up to request clarification or add additional context in comments.

6 Comments

BTW, the variable name filename is not good. filename is a file object, not the name of the file.
I get the error: Traceback (most recent call last): File "N:/Test.py", line 4, in <module> for item in list[:5]: TypeError: 'type' object is not subscriptable
@JoshF98 that was a typo on my part, it's fixed now.
If this answer helped you, please consider marking it as "Accepted". This gives both you and I a reputation bonus, and allows future readers to reference it.
So I don't get the I/O error anymore and it prints the scores on separate lines however when it writes to the file it does not write on separate line, it writes like: 29.429.21.42.92.3
|
0

You need to convert var to string:

filename.write(str(var))

1 Comment

None of these seem to work :( They all give out errors regarding the writing of the file...
0

You need to convert the float to string using str(L[i])

L = [29.4, 29.2, 1.4, 2.9, 2.3]
print (L)
x = len (L)
filename = open ('Scores.txt','rt')
if x == 5:
    for i in range (0,5):
        var = (str(L[i]))
        print(var)
        filename.write (var)
    filename.close()

Also, filename.close is a method, you need to call it with the parentheses

Note that its more standard to use the with statment

L = [29.4, 29.2, 1.4, 2.9, 2.3]
print (L)
x = len (L)
with open ('Scores.txt','rt') as filename:
    if x == 5:
        for i in range (0,5):
            var = (str(L[i]))
            print(var)
            filename.write (var)

That way python will automatically close the file, even if there is any exception

2 Comments

I get the following I/O error when running the code: Traceback (most recent call last): File "<my server goes here> Test.py", line 9, in <module> filename.write (var) io.UnsupportedOperation: not writable Could anyone help?
I know you got the answer already, just so you know, its because of the mode you opened the file in. "rt", which is "read text". You need "w" for "write" Check this part of the docs docs.python.org/3/library/functions.html#open. Btw, the "t" is not needed, since text is the default

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.