I am a new programmer and I wanted to start by writing a Tic-tak-Toe program I have more to add but here it is so far.

import random

board = """ 1 | 2 | 3
---+---+---
 4 | 5 | 6
---+---+---
 7 | 8 | 9 """;
print board

move = raw_input('Where do you want to place your X? ')

board = board.replace(move, "X");
print board

print 'Computer turn:'
comp = random.randint(1, 9)
board = board.replace(comp, "O");
print board

when I run it it shows this error:
Traceback (most recent call last):
File "/private/var/folders/fL/fL1lGxccGCypPBSIUXNGZGpkwIg/-Tmp-/Cleanup At Startup/a-343878441.391.py", line 17, in <module>
board = board.replace(comp, "O");
TypeError: expected a character buffer object


What does this mean, and how would I go about fixing it?

Recommended Answers

All 2 Replies

It means that the replace function expects a string (or string-like object) as the first parameter, but you passed it an integer. Line 17 should be board = board.replace(str(comp), "O") .

Note that the semicolons at lines 7, 12, 17 are not needed, though they are not illegal.

For example like

print 'Computer turn:'
comp = random.choice([c for c in board if c.isdigit()])

Otherwise you can choose same as user chose (also you should check that user's input is in board).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.