I have to create a program for one of my classes that will follow the basic rules of the game "Craps". This involves getting two random dice rolls and checking if the total shows for a win, a loss, or a reroll until a win or loss. That part isn't the issue at hand though. Where I am stuck is introducing a win/loss counter for these rolls. If someone could please guide me on implementing this into my code, or revising the code so it is possible to do so.
#Imports
import random
#Variable declaration
D1 = 0
D2 = 0
DTotal = 0
WinningValues = [7,11]
LosingValues = [2,3,12]
WinTotal = 0
LoseTotal = 0
def gameloop():
D1 = random.randint(1,6)
D2 = random.randint(1,6)
DTotal = D1 D2
print("You rolled", D1, "and", D2,"for a total of", DTotal)
if DTotal in WinningValues:
print("You win")
Cont = input("Type Y to roll again, X to quit, or S to see your stats: ")
craps(Cont)
elif DTotal in LosingValues:
print("You lose")
Cont = input("Type Y to roll again, X to quit, or S to see your stats: ")
craps(Cont)
else:
print("You roll again")
craps(Cont="Y")
def showstats(WinTotal, LoseTotal):
print("You won a total of",WinTotal,"and lost a total of",LoseTotal,"times.")
def craps(Cont):
if Cont == "Y":
gameloop()
if Cont == "S":
print("Executing")
showstats(WinTotal, LoseTotal)
if Cont == "X":
quit()
#Program running
Cont = input("Would you like to play a game of craps? Type Y to play or X to quit: ")
if Cont == 'Y':
gameloop()
elif Cont == 'X':
quit()
I have tried implementing WinTotal = 1
after print("You win")
but it comes back with a local variable referenced before assignment error that I have tried debugging but just haven't had any luck with it, let alone understanding it.
CodePudding user response:
some cases not added like..
- if someone pressed wrong key but he/she wanted to play again.
Code:-
import random
win,lose=0,0
asking=input("Would you like to play a game of craps? Type Y to play or any other key to quit: ")
while asking=="Y":
WinningValues = [7,11]
LosingValues = [2,3,12]
while True:
D1 = random.randint(1,6)
D2 = random.randint(1,6)
total=D1 D2
print("You rolled", D1, "and", D2,"for a total of", total)
if total in WinningValues:
print("You win")
win =1
break
elif total in LosingValues:
print("You lose")
lose =1
break
else:
print("You roll again")
asking=input("Type Y to roll again, S to see your stats, or any other key to exit: ")
if asking=="S":
print("You won a total of",win,"and lost a total of",lose,"times.")
asking=input("Type Y to roll again or any other key to quit: ")
if win or lose:
print("Your Stats are: ")
print("Win: " str(win))
print("Lose: " str(lose))
else:
print("You have not played any game")
Output:-
#Testcase1 When user not played any game..!
Would you like to play a game of craps? Type Y to play or any other key to quit: A
You have not played any game
#Testcase2 User played and wanted to know stats of his/her game and when exit also see his/her stats.
Would you like to play a game of craps? Type Y to play or any other key to quit: Y
You rolled 4 and 3 for a total of 7
You win
Type Y to roll again, S to see your stats, or any other key to exit: Y
You rolled 5 and 5 for a total of 10
You roll again
You rolled 6 and 4 for a total of 10
You roll again
You rolled 3 and 3 for a total of 6
You roll again
You rolled 1 and 4 for a total of 5
You roll again
You rolled 5 and 3 for a total of 8
You roll again
You rolled 1 and 2 for a total of 3
You lose
Type Y to roll again, S to see your stats, or any other key to exit: Y
You rolled 2 and 6 for a total of 8
You roll again
You rolled 6 and 4 for a total of 10
You roll again
You rolled 4 and 1 for a total of 5
You roll again
You rolled 1 and 1 for a total of 2
You lose
Type Y to roll again, S to see your stats, or any other key to exit: S
You won a total of 1 and lost a total of 2 times.
Type Y to roll again or any other key to quit: A
Your Stats are:
Win: 1
Lose: 2