Reputation: 3
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()
Upvotes: 0
Views: 102
Reputation: 1493
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
Upvotes: 0
Reputation: 4835
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.
Upvotes: 0
Reputation: 151
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.
Upvotes: 3