Question

In: Computer Science

fix this code in python and show me the output. do not change the code import...

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" )

Solutions

Expert Solution

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-


Related Solutions

Please fix all of the errors in this Python Code. import math """ A collection of...
Please fix all of the errors in this Python Code. import math """ A collection of methods for dealing with triangles specified by the length of three sides (a, b, c) If the sides cannot form a triangle,then return None for the value """ ## @TODO - add the errlog method and use wolf fencing to identify the errors in this code def validate_triangle(sides): """ This method should return True if and only if the sides form a valid triangle...
Python 3 Fix the code and rovide the correct indentation Code: import tkinter as tk from...
Python 3 Fix the code and rovide the correct indentation Code: import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook from tkinter import messagebox from datetime import datetime window = tk.Tk() window.title("daily logs") window.grid_columnconfigure(1,weight=1) window.grid_rowconfigure(1,weight=1) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="sold by").grid(row=3, sticky="W", pady=20, padx=20) tk.Label(window, text="Failed date").grid(row=4, sticky="W", pady=20, padx=20) # entries barcode = tk.Entry(window) product = tk.Entry(window) money =...
In Java please Cipher.java: /* * Fix me */ import java.util.Scanner; import java.io.PrintWriter; import java.io.File; import...
In Java please Cipher.java: /* * Fix me */ import java.util.Scanner; import java.io.PrintWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; public class Cipher { public static final int NUM_LETTERS = 26; public static final int ENCODE = 1; public static final int DECODE = 2; public static void main(String[] args) /* FIX ME */ throws Exception { // letters String alphabet = "abcdefghijklmnopqrstuvwxyz"; // Check args length, if error, print usage message and exit if (args.length != 3) { System.out.println("Usage:\n"); System.out.println("java...
Please fix this python code for me DOWN_PAYMENT_RATE = 0.10 ANNUAL_INTEREST_RATE = 0.12 MONTHLY_PAYMENTS_RATE = 0.05...
Please fix this python code for me DOWN_PAYMENT_RATE = 0.10 ANNUAL_INTEREST_RATE = 0.12 MONTHLY_PAYMENTS_RATE = 0.05 purchasePrice = float(input("Enter the purchase price: ")) month = 1 payment = purchasePrice * MONTHLY_PAYMENTS_RATE startingBalance = purchasePrice print("\n%s%19s%18s%19s%10s%17s" % ("Month", "Starting Balance", "Interest to Pay", "Principal to Pay", "Payment", "Ending Balance")) while startingBalance > 0:     interestToPay = startingBalance * ANNUAL_INTEREST_RATE / 12     principalToPay = payment - interestToPay     endingBalance = startingBalance - payment     print("%2d%16.2f%16.2f%18.2f%18.2f%15.2f" % (month, startingBalance, interestToPay, principalToPay, payment,...
Python 3 Calendar does not showing up Fix code: # required library import tkinter as tk...
Python 3 Calendar does not showing up Fix code: # required library import tkinter as tk from tkcalendar import DateEntry import xlsxwriter # frame window = tk.Tk() window.title("daily logs") #window.resizable(0,0) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="Failed date").grid(row=3, sticky="W", pady=20, padx=20) # entries barcode = tk.Entry(window) product = tk.Entry(window) money = tk.Entry(window) # arraging barcode.grid(row=0, column=1) product.grid(row=1, column=1) money.grid(row=2, column=1) cal = DateEntry(window, width=12, year=2019,...
Python 3 Fix the code. It is not saving the data into the xls file Code:...
Python 3 Fix the code. It is not saving the data into the xls file Code: import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook from tkinter import messagebox from datetime import datetime window = tk.Tk() window.title("daily logs") window.grid_columnconfigure(1,weight=1) window.grid_rowconfigure(1,weight=1) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="sold by").grid(row=3, sticky="W", pady=20, padx=20) tk.Label(window, text="Working product").grid(row=4, sticky="W", pady=20, padx=20) #Working product label tk.Label(window, text="Failed...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner;...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner; public class Booolean0p {        public class BooleanOp {            public static void main(String[] args) {                int a = 0, b = 0 , c = 0;                Scanner kbd = new Scanner(System.in);                System.out.print("Input the first number: ");                a = kbd.nextInt();                System.out.print("Input...
Python I am creating a class in python. Here is my code below: import csv import...
Python I am creating a class in python. Here is my code below: import csv import json population = list() with open('PopChange.csv', 'r') as p: reader = csv.reader(p) next(reader) for line in reader: population.append(obj.POP(line)) population.append(obj.POP(line)) class POP: """ Extract the data """ def __init__(self, line): self.data = line # get elements self.id = self.data[0].strip() self.geography = self.data[1].strip() self.targetGeoId = self.data[2].strip() self.targetGeoId2 = self.data[3].strip() self.popApr1 = self.data[4].strip() self.popJul1 = self.data[5].strip() self.changePop = self.data[6].strip() The problem is, I get an error saying:  ...
What does each line of this python code do? import datetime import getpass print("\n\nFinished execution at...
What does each line of this python code do? import datetime import getpass print("\n\nFinished execution at ", datetime.datetime.now()) print(getpass.getuser())
This is in Python: Alter your code for previous Do It Now problem, with the change...
This is in Python: Alter your code for previous Do It Now problem, with the change that the row should only be printed by the second number that the user enters. For example, the user enters two numbers of 5 and 7 for the two input lines in the code, and the following will be printed: 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT