PYTHON Craps is a dice-based game played in many casinos. Like blackjack, a play
ID: 3893602 • Letter: P
Question
PYTHON
Craps is a dice-based game played in many casinos. Like blackjack, a player plays against the house. The game starts with the player throwing a pair of standard, six-sided dice. If the player rolls a total of 7 or 11, the player wins. If the player rolls a total of 2, 3, or 12, the player loses. For all other roll values, the player will repeatedly roll the pair of dice until either she/he rolls the initial value again (in which case she/he wins) or 7 (in which case she/he loses).
1. Write a function craps that takes no parameters, simulates one game of craps, and returns 1 if the player won and 0 if the player lost. It should also print a history of the rolls so that the player can verify that the function is doing the right thing. The following shows several sample runs of the function:
>>> random.seed(1)
>>> craps()
2 5
1
>>> random.seed(2)
>>> craps()
1 1
0
>>> random.seed(9)
>>> craps()
4 5
3 3
2 2
6 1
0
>>> random.seed(7)
>>> craps()
3 2
4 6
1 1
5 1
3 5
1 5
2 1
1 4
1
Explanation / Answer
import random def craps(): initial_x=random.randrange(1,7) initial_y=random.randrange(1,7) x=initial_x y=initial_y print(str(x) + " " + str(y)) if x + y == 7 or x + y == 11: return 1 elif x + y == 2 or x + y == 3 or x + y == 12: return 0 else: while True: x=random.randrange(1,7) y=random.randrange(1,7) print(str(x)+" "+str(y)) if x+y==initial_y+initial_x: return 1 elif x+y==7: return 0 print(craps())
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.