# Assignment

Solution

pig (1).py

import random

user, computer = 0, 0 # the scores of user and computer

while True:

# user’s turn

print (“\nYour points = {}, computer’s points = {}\n”.format(user, computer))

points = 0

stopRolling = False

while not stopRolling:

# ask if the user wants to roll again

while not stopRolling:

choice = input(‘Enter r for roll again and h for hold: ‘)

choice = choice.strip().lower()

if choice == ‘r’: # roll again

break

if choice == ‘h’: # hold, thus return the points

stopRolling = True

break

ifstopRolling:

break

dice = random.randint(1, 6)

if dice == 1:

points = 0

stopRolling = True

else:

points += dice

print(“You rolled {}. Total points in this turn is {} so far.”.format(dice, points))

user += points

if user >= 100:

print (“\nYour points = {}, computer’s points = {}”.format(user, computer))

print (‘\nYou won!’)

break

# computer’s turn

print (“\nComputer’s Turn”)

points = 0

stopRolling = False

while not stopRolling and computer + points < 100 and points < 20:

dice = random.randint(1, 6)

if dice == 1:

points = 0

stopRolling = True

else:

points += dice

print(“Computer rolled {}. Total points in this turn is {} so far.”.format(dice, points))

computer += points

if computer >= 100:

print (“\nYour points = {}, computer’s points = {}”.format(user, computer))

print (‘\nComputer won!’)

break