sebastian sebastian - 1 month ago 14
Python Question

Repeat function, but when repeated; cleanest way to skip two call for functions

while True:
game_info(player_1, player_2, wins_1, wins_2)
wins_1, wins_2 = move(board, wins_1, wins_2) # move() sætter et 'tegn' på board og returner win1/win2


if wins_1 or wins_2 == 1:
new_round = input("Do you want to play again?\nType yes or no: ").lower()


if new_round == "yes" or new_round == "y": # hvis skal spille igen clear board.
board = [ ['| ', '| ', '| '], ['| ', '| ', '| '], ['| ', '| ', '| '] ]
print("_____________________________________________________________\n" \
"player_2: %s is going to start, with the sign: %s|" % (player_2, sign))


elif new_round == "no" or new_round == "n": # hvis ikke: print hvem der vandt flest runder
pass

else:
"""HERE I WANT IT TO REPEAT IT ALL EXCEPT FOR THE TWO FIRST "game_info", and "move" """


At the else statement, it should repeat the whole "while True" EXCEPT for the two first "game_info" and "move" functions. Which should be skipped.
That way end up right after the else statement, and ask the players for another round.

This isnt too hard of a problem though, in fact I could come up with a few solutions right away. But my question is, what is the cleanest way to solve this, and by that I mean how can I do this in the least code.

Lex Lex
Answer

I'd say you just add a boolean called skip (or anything) set to false outside the loop and change it in the else case.

Then move the functions to be excluded into an

if not skip:
    #Functions that might be skipped

Should do the job with low effort and in a clean way.