restarting game code - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: restarting game code (/thread-20866.html) |
restarting game code - zyada7med - Sep-03-2019 hi guys this is my code and i tried to make a game restart function but i couldn't can you help me please
RE: restarting game code - ichabod801 - Sep-03-2019 Can you show us what you did for the restart function/method? If you just want to reset the scores, what you need is right there at the start of play game. And please use Python tags instead of output tags. RE: restarting game code - zyada7med - Sep-03-2019 actually the game just close and i cant stop that (Sep-03-2019, 06:20 PM)ichabod801 Wrote: Can you show us what you did for the restart function/method? If you just want to reset the scores, what you need is right there at the start of play game.i try this but didnt work def win ( q = input("do you want to play again ? yes or no ?") if q == "yes" play_game() RE: restarting game code - ichabod801 - Sep-03-2019 First of all, that needs to be def win(self): . You need the closing parenthesis and the colon to have proper syntax. And assuming this is a method of Game, the self parameter will be automatically provided and you need to be ready for that.That does not reset your scores, but if you copy and paste the initial values of the scores from play_game, that will work. Mostly. I'm assuming you would call won from play_game, but then won calls play_game. This is called recursion (a function calling itself), and while it is supported in Python, there are limits to it in Python. I would avoid it unless there's a good reason. Instead, I would write a reset method that just reset the scores to 0. Then put the code from play_game in a while loop: while True: # play the game code here q = input("do you want to play again ? yes or no ?") if q == 'yes': self.reset() else: breakThat way the scores will be reset if they want to play again, and the program will end if they don't. RE: restarting game code - zyada7med - Sep-03-2019 where exactly i but this code and what exactly i copy from play_game im sorry if i bother you :" i just didnt understand well sorry again RE: restarting game code - ichabod801 - Sep-03-2019 I'm not going to write your code for you. Give it a try yourself. |