Question

In: Computer Science

complete the code to determine the highest score value in python import random #variables and constants...

complete the code to determine the highest score value in python

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" , "4 of a kind" ]

pScore = 0
cScore = 0

num_Score = int( input ("Enter a number of round: ") )
print ("\n")
count = 0
while count < num_Score:
#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]
#end if

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" )
print("\n")
pScore += 1
elif cmax > pmax:
print(" computer wins!", end="\n" )
print("\n")
cScore += 1
else:
print(" Tie!", end="\n")
print("\n")

  
count += 1
#end while

Solutions

Expert Solution

In a program when player wins the game then player score is incremented by 1, if computer wins the game then computer score is incremented by 1.

So here player score and computer score is used in order to determine the highest score value.

so for example if there are 5 rounds and player wins the game 2 times and computer wins the game 1 time and 2 times tie. so here player has score 2 and computer has score 1.

So player has the highest score than the computer so print highest score value is 2.

The program is given below:

import random

MAX_ROLLS = 5
MAX_DICE_VAL = 6

#declare a list of roll types

ROLL_TYPES = [ "Junk" , "Pair" , "3 of a kind" , "4 of a kind" ]

pScore = 0
cScore = 0

num_Score = int( input ("Enter a number of round: ") )
print ("\n")
count = 0
while(count<num_Score):
#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]
#end if

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" )
print("\n")
#if player wins the gamethen increment pScore by 1
pScore += 1
elif cmax > pmax:
print(" computer wins!", end="\n" )
print("\n")
#if computer wins the game then increment cScore by 1
cScore += 1
else:
print(" Tie!", end="\n")
print("\n")
  
count += 1
#end while

#print Player score
print("Player Score: ",pScore)
#print Computer score
print("Computer Score: ",cScore)
#if player has higher score than computer
if(pScore>cScore):
#print pScore
print("highest score value: ",pScore)
#if player and computer has same score
elif (pScore==cScore):
#print score either using pScore or cScore
print("highest score value: ",pScore)
#else
else:
#print cScore
print("highest score value: ",cScore)

The screenshot of code is gven below:

Output Example 1:

Output Example 2:


Related Solutions

Please convert this code written in Python to Java: import string import random #function to add...
Please convert this code written in Python to Java: import string import random #function to add letters def add_letters(number,phrase):    #variable to store encoded word    encode = ""       #for each letter in phrase    for s in phrase:        #adding each letter to encode        encode = encode + s        for i in range(number):            #adding specified number of random letters adding to encode            encode = encode +...
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:  ...
Need a python code for LU factorization( for partial pivoting and complete pivoting) of a random...
Need a python code for LU factorization( for partial pivoting and complete pivoting) of a random matrix size 5x5.
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...
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 =...
USING PYTHON 3.7: import Student as st import random THRESHOLD = 0.01 tests = 0 passed...
USING PYTHON 3.7: import Student as st import random THRESHOLD = 0.01 tests = 0 passed = 0 def main():    test_get_higher_grade_student()    test_is_grade_above()    test_get_students()    test_get_classlist()    # test_count_above()    # test_get_average_grade()    print("TEST RESULTS:", passed, "/", tests) def test_get_higher_grade_student():    s1a = st.Student("V0002000", 89)    s1b = st.Student("V0002001", 89)    s2 = st.Student("V0002002", 67)    s3 = st.Student("V0002002", 97)    result = get_higher_grade_student(s1a,s1b)    print_test("get_higher_grade_student(s1a,s1b)", result == s1a)    result = get_higher_grade_student(s1a,s2)    print_test("get_higher_grade_student(s1a,s1b)", result == s1a)...
Trying to score a hand of blackjack in this python code but my loop is consistently...
Trying to score a hand of blackjack in this python code but my loop is consistently outputting (13,1) which makes me think that something is wrong with my loop. Could someone help me with this code?: import random cards = [random.randint(1,13) for i in range(0,2)] #initialize hand with two random cards def get_card(): #this function will randomly return a card with the value between 1 and 13 return random.randint(1,13) def score(cards): stand_on_value = 0 soft_ace = 0 for i in...
JAVA ONLY - Complete the code import java.util.Scanner; /** * This program will use the HouseListing...
JAVA ONLY - Complete the code import java.util.Scanner; /** * This program will use the HouseListing class and display a list of * houses sorted by the house's listing number * * Complete the code below the numbered comments, 1 - 4. DO NOT CHANGE the * pre-written code * @author * */ public class HouseListingDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); HouseListing[] list; String listNumber, listDesc; int count = 0; double listPrice; String...
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())
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT