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 +...
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)...
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())
Programming Java Homework: Find the Highest Score (Just need the code and directions followed exactly and...
Programming Java Homework: Find the Highest Score (Just need the code and directions followed exactly and written in Java code) Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the name and score of the student with the highest score. Use the next () method in the Scanner class to read a name, rather than using the nextLine () method.
PYTHON PROBLEM: TASKED TO FIND THE FLAW WITH THE FOLLOWING CODE: from itertools import count def...
PYTHON PROBLEM: TASKED TO FIND THE FLAW WITH THE FOLLOWING CODE: from itertools import count def take_e(n, gen):     return [elem for (i, elem) in enumerate(gen) if i < n] def take_zc(n, gen):     return [elem for (i, elem) in zip(count(), gen) if i < n] FIBONACCI UNBOUNDED: def fibonacci_unbounded():     """     Unbounded Fibonacci numbers generator     """     (a, b) = (0, 1)     while True:         # return a         yield a         (a, b) = (b, a + b) SUPPOSED TO RUN THIS TO ASSIST WITH...
Python program.  from random import . Write a function called cleanLowerWord that receives a string as a...
Python program.  from random import . Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT