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!
You’re telling
thisLine.replaceto replace the string"genRANDOM"with the numbergenRandom. 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 of4.Also, you might want to name the variable something else;
genRandomsounds like a function that generates a random number to me, not a random number that’s been generated.