patternpythonMinor
Working Craps program
Viewed 0 times
crapsworkingprogram
Problem
How can I make this better?
import random
def roll():
input("Press Enter to roll")
dice = random.randint(1, 6) + random.randint(1, 6)
print("You rolled a {}" .format(dice))
return dice
def checkRoll(dice):
if dice == 7 or dice == 11:
print("You win")
playAgain()
elif dice == 2 or dice == 3 or dice == 12:
print("You lose")
playAgain()
else:
print("Time to try to roll the point")
reRoll(dice)
def reRoll(point):
dice = roll()
if dice == point:
print("You win")
playAgain()
elif dice == 7:
print("You lose")
playAgain()
else:
reRoll(point)
def play():
print("Do you wanna play Craps? Y/n ")
response = input(">>> ").lower()
if response == "y":
checkRoll(roll())
else:
input("ENTER to quit")
def playAgain():
response = input("Play Again? Y/n >>>").lower()
if response == "y":
checkRoll(roll())
else:
input("ENTER to quit")
play()Solution
PEP8
1.Python uses underscore as naming separator in function and variable names, see PEP8
2.Two blank lines are used to separate function/classes
Improvements.
To make your module reusable you should use
So instead of calling
Your play_again and play function got difference only in print statements, so you can just combine them into a single function.
Note
I've renamed function into
Now if you look at your check_roll and reroll function they are also the same except for win and lose conditions, so you can also combine them into a single one like this:
Also, I did small improvement to roll() function so now you can see values of dice you've rolled:
So in the end, what we have is this:
1.Python uses underscore as naming separator in function and variable names, see PEP8
2.Two blank lines are used to separate function/classes
Improvements.
To make your module reusable you should use
if __name__ == '__main__' condtion before invoking your main function.So instead of calling
play() you should do:if __name__ == '__main__':
play()Your play_again and play function got difference only in print statements, so you can just combine them into a single function.
def start_game(first_run=False):
if first_run:
print("Do you wanna play Craps? Y/n ")
response = input(">>> ").lower()
else:
response = input("Play Again? Y/n >>>").lower()
if response == "y":
check_roll(roll())
else:
input("ENTER to quit")Note
I've renamed function into
start_game since I find play a bit confusing. But you can keep your name if you want.Now if you look at your check_roll and reroll function they are also the same except for win and lose conditions, so you can also combine them into a single one like this:
def check_roll(dice, win_conditions=(7, 11), lose_conditions=(2, 3, 12)):
if dice in win_conditions:
print("You win")
start_game()
elif dice in lose_conditions:
print("You lose")
start_game()
else:
print("Time to try to roll the point")
check_roll(roll(), (dice,), (7,))Also, I did small improvement to roll() function so now you can see values of dice you've rolled:
def roll():
input("Press Enter to roll")
dice = random.randint(1, 6), random.randint(1, 6)
print("You rolled a {}" .format(dice))
return sum(dice)So in the end, what we have is this:
import random
def roll():
input("Press Enter to roll")
dice = random.randint(1, 6), random.randint(1, 6)
print("You rolled a {}" .format(dice))
return sum(dice)
def check_roll(dice, win_conditions=(7, 11), lose_conditions=(2, 3, 12)):
if dice in win_conditions:
print("You win")
start_game()
elif dice in lose_conditions:
print("You lose")
start_game()
else:
print("Time to try to roll the point")
check_roll(roll(), (dice,), (7,))
def start_game(first_run=False):
if first_run:
print("Do you wanna play Craps? Y/n ")
response = input(">>> ")
else:
response = input("Play Again? Y/n >>>")
if response.lower() == "y":
check_roll(roll())
else:
input("ENTER to quit")
if __name__ == '__main__':
start_game(True)Code Snippets
if __name__ == '__main__':
play()def start_game(first_run=False):
if first_run:
print("Do you wanna play Craps? Y/n ")
response = input(">>> ").lower()
else:
response = input("Play Again? Y/n >>>").lower()
if response == "y":
check_roll(roll())
else:
input("ENTER to quit")def check_roll(dice, win_conditions=(7, 11), lose_conditions=(2, 3, 12)):
if dice in win_conditions:
print("You win")
start_game()
elif dice in lose_conditions:
print("You lose")
start_game()
else:
print("Time to try to roll the point")
check_roll(roll(), (dice,), (7,))def roll():
input("Press Enter to roll")
dice = random.randint(1, 6), random.randint(1, 6)
print("You rolled a {}" .format(dice))
return sum(dice)import random
def roll():
input("Press Enter to roll")
dice = random.randint(1, 6), random.randint(1, 6)
print("You rolled a {}" .format(dice))
return sum(dice)
def check_roll(dice, win_conditions=(7, 11), lose_conditions=(2, 3, 12)):
if dice in win_conditions:
print("You win")
start_game()
elif dice in lose_conditions:
print("You lose")
start_game()
else:
print("Time to try to roll the point")
check_roll(roll(), (dice,), (7,))
def start_game(first_run=False):
if first_run:
print("Do you wanna play Craps? Y/n ")
response = input(">>> ")
else:
response = input("Play Again? Y/n >>>")
if response.lower() == "y":
check_roll(roll())
else:
input("ENTER to quit")
if __name__ == '__main__':
start_game(True)Context
StackExchange Code Review Q#149606, answer score: 3
Revisions (0)
No revisions yet.