Jump to content

Could be wrong, but the problem might be from using input as a variable as well as a function.  (Never used python before, but just as a quick "too look at" point).

 

Perhaps change

    input = input("Select a spot: ")    input = int(input)

To

    user_spot = int(input("Select a spot: "))

or something like that and see if you get an error

0b10111010 10101101 11110000 00001101

Link to comment
https://linustechtips.com/topic/43445-python-trouble-shooting/#findComment-567607
Share on other sites

Link to post
Share on other sites

Could be wrong, but the problem might be from using input as a variable as well as a function.  (Never used python before, but just as a quick "too look at" point).

 

Perhaps change

    input = input("Select a spot: ")    input = int(input)

To

    user_spot = int(input("Select a spot: "))

or something like that and see if you get an error

 
Traceback (most recent call last):
  File "C:\Users\Michael\Desktop\TicTacToe.py", line 43, in <module>
    if board[input] != 'x' and board [input] != 'o':
TypeError: list indices must be integers, not builtin_function_or_method
 
:(
Link to comment
https://linustechtips.com/topic/43445-python-trouble-shooting/#findComment-567695
Share on other sites

Link to post
Share on other sites

In python you cannot use predefined function names, or key-words as variables.  Change your 'input' variable to something else such as 'userInput'.

while True:    userInput = input("Select a spot: ")    userInput = int(userInput)    if board[userInput] != 'x' and board [userInput] != 'o':        board[userInput] = 'x'
Link to comment
https://linustechtips.com/topic/43445-python-trouble-shooting/#findComment-567724
Share on other sites

Link to post
Share on other sites

 

In python you cannot use predefined function names, or key-words as variables.  Change your 'input' variable to something else such as 'userInput'.

while True:    userInput = input("Select a spot: ")    userInput = int(userInput)    if board[userInput] != 'x' and board [userInput] != 'o':        board[userInput] = 'x'

Ahh, I did not know that. 

 

Thanks it is now working :) could there be an easier way to do the checkAll function? I'm too tired to think :P

Link to comment
https://linustechtips.com/topic/43445-python-trouble-shooting/#findComment-568053
Share on other sites

Link to post
Share on other sites

Ahh, I did not know that. 

 

Thanks it is now working :) could there be an easier way to do the checkAll function? I'm too tired to think :P

 

There aren't any significantly better ways of doing it, but I can think of one method which might look slightly more elegant than a series of if statements.  

winningRows = [[0,1,2],               [3,4,5],               [6,7,8],                [0,3,6],               [1,4,7],               [2,5,8],               [0,4,8],               [2,4,6]] def checkAll (char):    for row in winningRows:        if [board[x] for x in row] == [char, char, char]:            return True

What I am doing here is creating a list of all winning solutions (or each possible line on a grid).  Then I am looping through each line, creating a list of values that fall on that line, and finally testing if that list of values is equal to a list of 3 "X"'s (or "O"'s) signifying a completed line.  

(Sorry if that was hard to understand, I am not that good at explaining things)

Link to comment
https://linustechtips.com/topic/43445-python-trouble-shooting/#findComment-568534
Share on other sites

Link to post
Share on other sites

There aren't any significantly better ways of doing it, but I can think of one method which might look slightly more elegant than a series of if statements.  

winningRows = [[0,1,2],               [3,4,5],               [6,7,8],                [0,3,6],               [1,4,7],               [2,5,8],               [0,4,8],               [2,4,6]] def checkAll (char):    for row in winningRows:        if [board[x] for x in row] == [char, char, char]:            return True

What I am doing here is creating a list of all winning solutions (or each possible line on a grid).  Then I am looping through each line, creating a list of values that fall on that line, and finally testing if that list of values is equal to a list of 3 "X"'s (or "O"'s) signifying a completed line.  

(Sorry if that was hard to understand, I am not that good at explaining things)

Not at all, I completely understand now :) Thank you and on to my next Python project :D

Link to comment
https://linustechtips.com/topic/43445-python-trouble-shooting/#findComment-568815
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×