Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
restarting game code
#4
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:
      break
That way the scores will be reset if they want to play again, and the program will end if they don't.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
restarting game code - by zyada7med - Sep-03-2019, 06:07 PM
RE: restarting game code - by ichabod801 - Sep-03-2019, 06:20 PM
RE: restarting game code - by zyada7med - Sep-03-2019, 06:47 PM
RE: restarting game code - by ichabod801 - Sep-03-2019, 07:51 PM
RE: restarting game code - by zyada7med - Sep-03-2019, 08:49 PM
RE: restarting game code - by ichabod801 - Sep-03-2019, 09:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Asking for help in my code for a "Guess the number" game. Domz 5 3,845 Aug-14-2019, 12:35 PM
Last Post: perfringo
  Code: Creating a basic python game? searching1 5 3,486 Nov-12-2018, 05:18 AM
Last Post: searching1
  Hangman-Game (German code) .. Unkreatief 1 3,741 Mar-22-2017, 10:30 AM
Last Post: Kebap

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020