In: Computer Science
fix this code in python and show me the output. do not change the code
import random
#variables and constants
MAX_ROLLS = 5
MAX_DICE_VAL = 6
#declare a list of roll types
ROLLS_TYPES = [ "Junk" , "Pair" , "3 of a kind" , "5 of a kind" ]
#set this to the value MAX_ROLLS
pdice = [0,0,0,0,0]
cdice = [0,0,0,0,0]
#set this to the value MAX_DICE_VAL
pdice = [0,0,0,0,0,0]
cdice = [0,0,0,0,0,0]
#INPUT - get the dice rolls
i = 0
while i < MAX_ROLLS:
pdice[i] = random.randint(1, MAX_DICE_VAL)
cdice[i] = random.randint(1, MAX_DICE_VAL,)
i += 1
#end while
#print the player's and computer dice rolls
i = 0
print ( "Player rolled: ", end=" " )
while i < MAX_ROLLS:
print( pdice[i], end = " " )
i += 1
#end while
print ("\n")
i = 0
print ("Computer rolled: ", end=" " )
while i < MAX_ROLLS:
print( cdice[i], end = " " )
i += 1
#end while
#load the tally list so we can determine pair, 3 of a kind,
etc
i = 0
while i < MAX_ROLLS:
ptally [ pdice[i] - 1 ] += 1
ctally [ cdice[i] - 1 ] += 1
i += 1
#end while
# find out pair, 3 of kind, etc
pmax = ptally[0] # init to first element in tally array
cmax = ctally[0]
i = 1
while i < MAX_DICE_VAL:
if pmax < ptally[i]:
pmax = ptally[i]
#end if
if cmax < ctally[i]:
cmax = ctally[i]
i += 1
#end while
#output - display what was rolled and who won
print("\n")
print(" player rolled: " + ROLL_TYPES[ pmax - 1], end="\n" )
print(" computer rolled: " + ROLL_TYPES[ cmax - 1] )
# determine the winner
if pmax > cmax:
print( "player wins!", end="\n" )
elif cmax > pmax:
print (" computer wins!", end="\n" )
else:
print("Tie!", end="\n" )
The errors were---
1) Indentation error - Indentations were not applied to if else and while loops throughout the program.
2)Index out of range errors - As ptally and ctally were not defined before the while loop and the maximum index value used for ctally and ptally is 5 that is from 0 to 5 so ctally and ptally must have 6 elements.
3)ROLL_TYPES is used in the program but ROLLS_TYPES is defined in the beginning so error showed during compiling.
============================================================
ERROR FREE PROGRAM-
import random
#variables and constants
MAX_ROLLS = 5
MAX_DICE_VAL = 6
#declare a list of roll types
ROLL_TYPES = [ "Junk" , "Pair" , "3 of a kind" , "5 of a kind" ]
#set this to the value MAX_ROLLS
pdice = [0,0,0,0,0]
cdice = [0,0,0,0,0]
#set this to the value MAX_DICE_VAL
pdice = [0,0,0,0,0,0]
cdice = [0,0,0,0,0,0]
#INPUT - get the dice rolls
i = 0
while i < MAX_ROLLS:
pdice[i] = random.randint(1, MAX_DICE_VAL)
cdice[i] = random.randint(1, MAX_DICE_VAL)
i += 1
#end while
#print the player's and computer dice rolls
i = 0
print ( "Player rolled: ", end=" " )
while i < MAX_ROLLS:
print( pdice[i], end = " " )
i += 1
#end while
print ("\n")
i = 0
print ("Computer rolled: ", end=" " )
while i < MAX_ROLLS:
print( cdice[i], end = " " )
i += 1
#end while
#load the tally list so we can determine pair, 3 of a kind,
etc
i = 0
ptally = [0,0,0,0,0,0]
ctally = [0,0,0,0,0,0]
while i < MAX_ROLLS:
ptally[ pdice[i] - 1 ] += 1
ctally[ cdice[i] - 1 ] += 1
i += 1
#end while
# find out pair, 3 of kind, etc
pmax = ptally[0] # init to first element in tally array
cmax = ctally[0]
i = 1
while i < MAX_DICE_VAL:
if pmax < ptally[i]:
pmax = ptally[i]
#end if
if cmax < ctally[i]:
cmax = ctally[i]
i += 1
#end while
#output - display what was rolled and who won
print("\n")
print(" player rolled: " + ROLL_TYPES[ pmax - 1], end="\n" )
print(" computer rolled: " + ROLL_TYPES[ cmax - 1] )
# determine the winner
if pmax > cmax:
print( "player wins!", end="\n" )
elif cmax > pmax:
print (" computer wins!", end="\n" )
else:
print("Tie!", end="\n")
======================================================
Output Pictures-