0

Below is the code that I have built for the dice game i made. I need it to display the number of wins and the number of losses once i quit the game. Everything is running smoothly, but it is only counting the last result currently. any ideas why?

import random

   def roll_dice():
      winner = 0
      loser = 0
      roll = (random.randint(1,6))
      roll2 = (random.randint(1,6))
      print(roll,"  "roll2)
      if roll == roll2:
        print("Winner!")
        winner += 1
      else:
        print("Loser!")
        loser += 1
      play_again = input("Would you like to play again?")

      if play_again == "yes":
        roll_dice()

      else:
        print("You won " , winner , "times")
        print("You lost ", loser , "times")
        quit

   def main():

     game_start = input("Would you like to roll the dice?")
     if game_start == 'yes':
        roll_dice()

     else:
        print("too bad")

   if __name__ == '__main__':
   main()
CptCS
  • 3
  • 1
  • Please add the current output and the desired output – RishabhHardas Mar 19 '19 at 04:22
  • 2
    You're resetting `winner` and `loser` every time you execute `roll_dice()`. Change those variables to global variables (defined in main or outside of a function) and then write `global winner, loser` at the top of `roll_dice`. – Preston Hager Mar 19 '19 at 04:23
  • `winner` and `loser`, will reset on every call to the function `roll_dice`, making them global or passing them to the function on every call would help, or you can use a class and make them the class variables. – RishabhHardas Mar 19 '19 at 04:26

3 Answers3

3

This is happening because you are variables winner and loser are local variables and are re-initialized to zero every time roll_dice() is called. You can either have winner and loser as global variables or pass them as arguments to your function. See below with global variable

import random

winner = 0
loser = 0

def roll_dice():
  global winner, loser
  roll = (random.randint(1,6))
  roll2 = (random.randint(1,6))
  print(roll,"  ",roll2)
  if roll == roll2:
    print("Winner!")
    winner += 1
  else:
    print("Loser!")
    loser += 1
  play_again = input("Would you like to play again?")

  if play_again == "yes":
    roll_dice()

  else:
    print("You won " , winner , "times")
    print("You lost ", loser , "times")
    quit

def main():

 game_start = input("Would you like to roll the dice?")
 if game_start == 'yes':
    roll_dice()

 else:
    print("too bad")

if __name__ == '__main__':
    main()

Also I would like to point out that you should not use recursion here as it can cause a stack overflow error. looping would be a better option.

  • thank you, I did not realize it was initializing the variables each time but explaining it that way helps a lot – CptCS Mar 19 '19 at 04:36
0

This is because winner and loser are local variables scoped to a call of roll_dice.

Each call would instantiate a new variable loser and winner and the instance goes away with the return of the function.

More here Short description of the scoping rules?

One dirty fix to your problem would be to make winner and loser variables in global scope.

A cleaner solution would be to restructure your code to make roll_dice return the count of winner and loser as a tuple.

bashrc
  • 4,725
  • 1
  • 22
  • 49
  • i am going with the dirty fix for now but i will look into how to incorporate the cleaner solution for future code to ensure my programs are efficient. thanks for your reply! – CptCS Mar 19 '19 at 04:38
0

Every time you call your function (because you made it as recursion), the variable of winner and loser become zero. I think you need to change your function in def roll_dice(). It's better to use looping as stated by @ashutoshy.and01.

Also, you make the player as a winner if only the number on both dices equal, otherwise a loser even the player's dice number is greater than the computer's.

def roll_dice():
    winner = 0
    loser = 0
    play_again = "yes"
    while play_again == "yes":
        roll = (random.randint(1,6))
        roll2 = (random.randint(1,6))
        print(str(roll),"  ",end=str(roll2))
        if roll >= roll2:
            print(" Winner!")
            winner += 1
        else:
            print(" Loser!")
            loser += 1
        play_again = input("Would you like to play again?")
    print("You won " , winner , "times")
    print("You lost ", loser , "times")
    quit
YusufUMS
  • 1,506
  • 1
  • 12
  • 24
  • this is much cleaner code and also fixes my problem without having to make 'winner' and 'loser' global variables so thanks so much – CptCS Mar 19 '19 at 04:46
  • You're welcome. You can mark the answer as accepted if you feel the code helps you. Cheers.. – YusufUMS Mar 19 '19 at 04:49