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 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)