Just for fun, to give you some ideas, here’s one way you could structure your game, that does the same thing as the code above but in less than thirty lines, versus the over 150 above. Combined with that, you can store your cell contents as a nested list, rather than in individual variables, which also simplifies things greatly.Īlso, some indents use four spaces and some use two, which combined with all the repetition, makes it even harder to follow and easier to not spot when you’ve missed an indent (as happened here) I suggest being consistent and always using four spaces, as is the standard for Python.įinally, make sure to never use a bare except (unless you know exactly what you’re doing) always use at least except Exception, as otherwise you’ll be catching KeyboardInterrupt and other such non-error exceptions, which makes it impossible for users to quit out of the game one function to print the board, one function to handle the input, one function to process the moves, and a main function to run your loop and call the other functions, your code can be many times shorter and easier to maintain. To note, the code contains a huge amount of repetition, and many layers of nested statements, which makes these errors much easier to make and much hard to spot, as well as your code much harder to understand, fix and improve-just by breaking things up into functions, e.g. Copying that over, everything works, with one exception: the very first player = 1' statement under the player = 2 block has a stray single quote character '. In this case, your player = 1 block has a try block, but is missing the corresponding except block that Python expects (or, else, a finally block), which your player = 2 block has. In particular, if Python thinks your indent level is wrong, then something above must be missing a block that Python expects. While the error message highlights the beginning of the second player’s block, as is often the case, the true error is on a line well above. Welcome, Brian! We’ll try not to be too hard on you If xcol > 0 and xcol 0 and xrow 0 and ocol 0 and orow < 4: Xrow = int(input('In which row would you like your X?')) Xcol = int(input('In which column would you like your X?')) Everything worked fine for player one, but now it’s giving me an error. ![]() I know to the experienced folk it will look like a big mess, but I’m self-taught and new, so please be gentle… in line 88, which reads “if player =2:”, I get an ‘unexpected unindent’ error, but I am not sure why. The following is something I’ve been working on.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |