In: Computer Science
Do the following code by using python: ( Thank!)
You roll two six-sided dice, each with faces containing one, two, three, four, five and six spots, respectively. When the dice come to rest, the sum of the spots on the two upward faces is calculated.
• If the sum is 7 or 11 on the first roll, you win.
• If the sum is 2, 3 or 12 on the first roll (called “Mygame”), you lose (i.e., the “house” wins).
• If the sum is 4, 5, 6, 8, 9 or 10 on the first roll, that sum becomes your “point.”
To win, you must continue rolling the dice until you “make your point” (i.e., roll that same point value). You lose by rolling a 7 before making your point.
The Python code for the given problem is as follows -
import random
point = 0
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
#After first roll
diceSum = dice1 + dice2
if(diceSum == 7 or diceSum == 11):
print('You Win!')
elif(diceSum == 2 or diceSum == 3 or diceSum == 12):
print('You Lose!')
else:
point = diceSum
while(True):
#Roll the dices again and agin
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
if (dice1+dice2 == point):
print('You Win')
break
if(dice1+dice2 == 7):
print('You Lose')
break
Explanation-
OUTPUT Screenshot-
We run the program multiple times and can see the different statements. To be more verbose, you can also print the diceSum value.
If you get multiple times the same value, then print and see the diceSum, which will surely be random.