0

Tic tac toe begginers project

def rev_play(moves, place): """ Docstring Author: Ache Description: Verifies if the place where the user is marking it's already in use Input: It recieves the list made from the plays and the place on the borard where the player is making the move Output: If the place where the player maker is already in use it will ask again to make chose another place iff it's not in use it'll return the place where the user marked Version:1.0 """ while moves[place] == 'O' or moves[place] == 'X': place = (int(input ('The place is already in use, choose another spot on the board: '))) else: break return place if I get rd of the else, break it works but it prints twice when my awful logic says it should be printing only once, why is it happening? Also, in this case with the else it's giving me a "SyntaxError: 'break' outside loop" why? As far as i undesrtand i'm using the correct sintax, ain't I?

15th Nov 2019, 10:08 PM
Hugo Bustamante
Hugo Bustamante - avatar
6 Respuestas
+ 3
My suggestion in code: def is_valid_input(moves, place): if moves[place] in ('O', 'X'): return False else: return True place = int(input('Enter a spot')) while not is_valid_input(moves, place): place = int(input('Wrong entry, try again')) print(f'Your correct move is {place}')
15th Nov 2019, 11:14 PM
Tibor Santa
Tibor Santa - avatar
+ 2
I don't fully understand tha last things you said, but if you're asking about the syntax error, it's because break cannot be used outside of the loops, it main use is to stop a loop, if there is no loop, it will produce an error. Also, why all this mess ? I don't think you really need to make a function for this simple task, why not write it directly in your program ?
15th Nov 2019, 10:16 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
It seems you are trying to use something like while-else-break statement. This is kind of valid in python but very rarely used, and you would put the break inside the while body, not after 'else'. The way you wrote it doesn't make much sense. You mention printing twice, but you did not post any code that implies any print at all. You should put this in bigger context, I suggest you save the whole code on the SL playground and post it here.
15th Nov 2019, 10:30 PM
Tibor Santa
Tibor Santa - avatar
+ 1
Woow, thats such a clean code, the thing is i was not using booleans, but i for sure i'm gonna use them next time, i just found out what the error was an i facepalmed my self, the dumbest thing, i was calling the function twice! So the issue was not on rev_play(moves, place) but outside of it, At least i learn a tons of stuff through this, first i will share my code as you said on the SL playground next time that i be having troubles like so, second there are much better ways to code stuff just as the code you kindly wrote, and third the errors can be coming from outside a piece of code, not just from the code one is looking at so i'll check it way better next time, thank you so much for the help.
15th Nov 2019, 11:49 PM
Hugo Bustamante
Hugo Bustamante - avatar
0
Another suggestion in general. When you write a function try to design it in a way that is really clear and focused. This function is really a mix of validation, and input / output. I would put all input in the main program, including the while loop, and do only the validation inside the function, and return True or False.
15th Nov 2019, 10:34 PM
Tibor Santa
Tibor Santa - avatar
0
What I mean is that basically the while, in the test that i'm running, it should be doing 1 single iteration that uses: place = (int(input ('The place is already in use, choose another spot on the board: '))) the second iteration should be jumpin, according to what i understand from my, obviously, incorrect logic, to the "else" part. I got rid of the sintaxis error deleting the "break" since the intention is just to return the new "place" so if i get it right it's doing 3 iterations (two in while and one in else) instead of 2 (one in while and one in else), that's why it prints twice the place = (int(input ('The place is already in use, choose another spot on the board: ')))
15th Nov 2019, 11:00 PM
Hugo Bustamante
Hugo Bustamante - avatar