0

How can I make it into a function?

Hey, This is a game of "4 in a row", using python (No graphics). The whole code is working well except of the function that checks whenever someone wins. In the function "check_win()", lines 83-103 & 106-126 : how can i make the part that is inside the 2 nested for-loops (the third one) - a function that will work for both the cases - they are the same - the third loop. code: https://code.sololearn.com/ca2023A123A2

21st Feb 2021, 1:11 PM
Yahel
Yahel - avatar
2 ответов
0
The code below should work. The global declaration permits you to modify variables that are defined outside the function. In this case the code is updating the X and O streak counts, so the function needs to declare those variables as global. def count_streaks(): global count_streak_X global count_streak_O for k in list_of_column: if k == 'X': count_streak_X += 1 count_streak_O = 0 check_if_4() elif k == 'O': count_streak_O += 1 count_streak_X = 0 check_if_4() else: count_streak_X = 0 count_streak_O = 0 Add this function inside the check_win() function, and replace the inline code with the single statement, check_streaks() in both places. Let us know if it resolves your question.
22nd Feb 2021, 10:33 PM
Brian
Brian - avatar
0
Brian, thanks for answering, I tried that many times and it's still not working... for some reason - when its separate: its working, and when its a function and you call it where you need to: its not working...
23rd Feb 2021, 8:58 AM
Yahel
Yahel - avatar