0

In Python, I open a text file and read it line-by-line, and strip() the line before evaluating it. When I evaluate the line, I have an if statement to check if the line is "random", and then puts a random number into a variable called genRandom. I have another line in my code that looks like this:

thisLine.replace("genRANDOM",genRandom) #Replace genRANDOM with the random number

On every line, it seems to work ok. In the input text file, I have a line that looks like this:

-genRANDOM

Whenever my script evaluates that line, I get this error:

Traceback (most recent call last):
  File "D:\Mass Storage\pythonscripts\TurnByTurn\execute.py", line 37, in <module>
    thisLine.replace("genRANDOM",genRandom) #Replace genRANDOM with the random number
TypeError: expected a character buffer object

How can I fix this? Thanks in advance!

0

1 Answer 1

2

You're telling thisLine.replace to replace the string "genRANDOM" with the number genRandom. That doesn't work, because it "expects a character buffer object" (such as a string), not a number.

Try str(genRandom) instead, to turn it into a string: "4" instead of 4.

Also, you might want to name the variable something else; genRandom sounds like a function that generates a random number to me, not a random number that's been generated.

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

2 Comments

genRandom is a variable. As I said, it's a randomly generated number. str(genRandom()) worked, though, thanks!
@ghostmancer Yeah, sorry, I missed that part of the question originally; like I say in my edit, genRandom strongly sounds more like a function to me than a number. :)

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.