Python code problem

Soldato
Joined
22 Nov 2007
Posts
4,091
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']
 
Associate
Joined
11 Dec 2016
Posts
2,018
Location
Oxford
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
 
Soldato
OP
Joined
22 Nov 2007
Posts
4,091
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.
 
Associate
Joined
11 Dec 2016
Posts
2,018
Location
Oxford
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
 
Soldato
OP
Joined
22 Nov 2007
Posts
4,091
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
 
Soldato
OP
Joined
22 Nov 2007
Posts
4,091
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.
 
Man of Honour
Joined
19 Oct 2002
Posts
29,515
Location
Surrey
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?
 
Soldato
OP
Joined
22 Nov 2007
Posts
4,091
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
 
Man of Honour
Joined
19 Oct 2002
Posts
29,515
Location
Surrey
Yes I use the Pro version myself as I have an All Products license. The good thing about the Jetbrains licenses is that they are perpetual. So if you stop paying then you still have perpetual access to the last version you paid for. You just don't get any new updates.
 
Soldato
OP
Joined
22 Nov 2007
Posts
4,091
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.
 
Soldato
Joined
27 Mar 2016
Posts
7,246
Location
Bristolian living in Swindon
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:
 
Soldato
OP
Joined
22 Nov 2007
Posts
4,091
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!
 
Soldato
Joined
13 Jan 2003
Posts
23,657
With most modern IDEs the curls are managed through code style tools (my preference is on the same line so dense code). The auto reformat of the code style sorts and indentation before checkin.
Code now tends to be more data driven without large hardcoded trees (if/switch/jump table). So is suppose you can get away with indentation of smaller functions and code structure for data driven systems (compared to 1970s/1980s when C ingrained the need for curly brackets).
 
Back
Top Bottom