Python code problem

Soldato
Joined
22 Nov 2007
Posts
4,204
Hey guys

I am writing code for a tic tac toe game as part of a udemy course i am doing. The problem is I can't get the following two functions to work properly. When the board is filled with X's or O's it works but if there are numbers in there it still returns True. The numbers are needed at the start so the player knows where they are putting X or O.


Code:
def space_check(board, position):
# print(board[position]=="X" or board[position]=="O")
 return board[position] != 'X' and board[position] != 'O'
 
 def full_board(board):
  for i in range(len(board)):
      if space_check(board, i): print(i)   
      return False
    print(i)
    return True
test_board = ['X', '1', '3', '4', '5', "6", 'X', 'O', 'X', 'O']
 
I guessed you want full_board(test_board) to return False when there are any numbers in it?
then the culprint is bad indentation on last return True which should be outside the for loop. Fucnction always returns true after checking only first position. You want it to return True only after looping through all items.

cleared it up indentation
Code:
def space_check(board, position):
    return board[position] != 'X' and board[position] != 'O'
 
def full_board(board):
    for i in range(len(board)):
        if space_check(board, i):
            print(i)  
            return False
        print(i)
    return True
test_board = ['X', '1', '3', '4', '5', "6", 'X', 'O', 'X', 'O']
assert full_board(test_board) == False

Ah brilliant. Only been learning for 3 weeks so still early days for me.
I was so frustrated last night trying to get this to work so thank you very much.
 
No problem
Whitespace catches out even seasoned programmers when they first try Python

An editor with Python syntax support would highlight such issues
If you will be coding for a while, I recommend free edition of Jetbrains PyCharm

I am using Jupyter notebooks for the course I am doing. Pycharm came with Anaconda, so yes i will eventually start to use that.

Thanks again
 
Jupyter is popular with the data science and machine learning community as its dynamic and encourages trial and error, but for maintainable and reusable code, I would move over to pycharm.

Yeah I'm going to buy the pro version that i have installed at the moment once my trial period is up. I think it was about £60 for a years license, not too bad.
 
Pycharm Community version is free. It might offer what you need instead of paying for the Professional version?

Thanks for the suggestion but i got the pro version yesterday. It has a lot more features that I will make use of(bold features in pro only)

PyCharm Professional Edition PyCharm Community Edition
Intelligent Python editor
Graphical debugger and test runner
Navigation and Refactorings
Code inspections
VCS support
Scientific tools
Web development
Python web frameworks
Python Profiler
Remote development capabilities
Database & SQL support
 
Hi @Bassmansam

How did you get on with this course in the end on Udemy? I am really enjoying studying Python and want to learn it big time but struggling to find a course/website to use...

If you could fire me any details about the site you used and any helpful information please that would be great

Hi Minibiker
It's a really good course. I'm about 75% through with it, i would have finished it but i got a new job in November as an anaylst and i barely did any of the course. I'm actually planning on resuming it for a 2/3 hours per week and I think i'll finish around October/November.


This is the course i'm doing, i think i paid about £20, udemy always seem to have a sale on though so don't pay too much!
Let me know if you have any further questions.
 
Fair play to you bud, hope you complete the final bit with no issues... I have gone for the Complete python course for £16... Hopefully get good at it and gain some decent experience for a job... I will have lots of questions over time I'm sure :cry:

I've done a fair bit of the basics over the last month or two but then it starts getting into more detail and I'm mind blown :cry:

Nice, its very rewarding when you begin to understand and write your own code and it works!

Just got to persevere really. Yeah feel free to answer questions, good luck!
 
Back
Top Bottom