Python Practice Sample:
Write a program to simulate rolling a 6-sided die until the sum of the numbers rolled is equal to or greater than 21. If it is exactly 21, print “You won!”; if it is more than 21, print “You lost!.” Show the number rolled and the accumulated total each time.
Number rolled: 6. Your total is now: 6.
Number rolled: 6. Your total is now: 12.
Number rolled: 4. Your total is now: 16.
Number rolled: 1. Your total is now: 17.
Number rolled: 6. Your total is now: 23.
You lost.
from random import randint total = 0 while total < 21: n = randint(1, 6) total += n print("Number rolled: {}. Your total is now: {}.".format(n, total)) if total == 21: print("You won!") else: print("You lost!")
Get Answers For Free
Most questions answered within 1 hours.