1

I am reading a number from a text file and trying to convert it into an int but it keeps giving me errors. This is my code:

f = open('commentcount.txt', 'r')
counts = f.readline()
int(counts)
counts = counts + 1
print(counts)

I am getting this error:

  counts = counts + 1
TypeError: Can't convert 'int' object to str implicitly

What am I doing wrong?

1 Answer 1

5

You must assign the value of int(counts) to counts in order to keep changes. Note that int(...) doesn't modify the variable you pass in.

counts = int(counts)

Be sure that f.readline() return an string that "represents" a int.

Sign up to request clarification or add additional context in comments.

Comments

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.