Question

In: Computer Science

"""    CS 125 - Intro to Computer Science    File Name: CS125_Lab1.py    Python Programming...

"""
   CS 125 - Intro to Computer Science
   File Name: CS125_Lab1.py
   Python Programming
   Lab 1

   Name 1: FirstName1 LastName1
   Name 2: FirstName2 LastName2
   Description: This file contains the Python source code for deal or no deal.
"""

class CS125_Lab1():
def __init__(self):
# Welcome user to game
print("Let's play DEAL OR NO DEAL")
  
# Define instance variables and init board
self.cashPrizes = [.01, .50, 1, 5, 10, 50, 100, 250, 500, 1000, 5000, 10000, 100000, 500000, 1000000]
self.remainingPrizesBoard = []
self.gameOver = False
self.offerHistory = []
self.initializeRandomPrizeBoard()


"""----------------------------------------------------------------------------
Prints the locations available to choose from
(0 through numRemainingPrizes-1)
----------------------------------------------------------------------------"""
def printRemainingPrizeBoard(self):
# Copies remaining prizes ArrayList into prizes ArrayList (a temp ArrayList)
prizes = []
for prize in self.remainingPrizesBoard:
prizes.append(prize)
  
prizes.sort()
  
# TODO 1: Print the prizes in the prize ArrayList. All values should be on
# a single line (put a few spaces after each prize) and add a new line
# at the end (simple for loop);
  
# NOTE: '${:,.2f}'.format(num) is the python
# equivalent to the Java df.format(num) DecimalFormat class, which allows
# you to print a decimal num like 5.6 as $5.60.

  
"""----------------------------------------------------------------------------
Generates the banks offer. The banker algorithm computes the average
value of the remaining prizes and then offers 85% of the average.
----------------------------------------------------------------------------"""
def getBankerOffer(self):
pass
# TODO 2: Write code which returns the banker's offer as a double,
# according to the description in this method's comment above.

#----------------------------------------------------------------------------
# Takes in the selected door number and processes the result
#----------------------------------------------------------------------------
def chooseDoor(self, door):
# TODO 6: Add the current bank offer (remember, we have a method
# for to obtain the current bank offer - call self.getBankerOffer())
# to the our offerHistory.
if door == -1:
pass
# This block is executed when the player accepts the banker's offer. Thus the game is over.
  
# TODO 3: Set the gameOver variable to true
# Inform the user that the game is over and how much money they accepted from the banker.
# Print the offer history (there is a method to call for this).
else:
pass
# This block is executed when the player selects one of the remaining doors.
  
# TODO 4: Obtain the prize behind the proper door and remove the prize from the board
# Print out which door the user selected and what prize was behind it (this prize is now gone)

         
# If only one prize remaining, game is over!!!
if len(self.remainingPrizesBoard) == 1:
pass
# This block is executed when there is only one more prize remaining...so it is what they win!
  
# TODO 5: Set the gameIsOver variable to true
# Let the user know what prize they won behind the final door!
# Print the offer history (there is a method to call for this).


"""----------------------------------------------------------------------------
This method is called when the game is over, and thus takes as a
parameter the prize that was accepted (either from the banker's offer
or the final door).
  
Prints out the offers made by the banker in chronological order.
Then, prints out the money left on the table (for example, if the
largest offer from the banker was $250,000, and you ended up winning
$1,000, whether from a later banker offer or from the last door,
then you "left $249,000 on the table).
----------------------------------------------------------------------------"""
def printOfferHistory(self, acceptedPrize):
# Print out the history of offers from the banker
  
# TODO 7: Print out the banker offer history (will need to loop through
# your offerHistory member variable) and find the max offer made.
# Print one offer per line, like:
# Offer 1: $10.00
# Offer 2: $5.00
# .....
maxOffer = 0
print("\n\n\n***BANKER HISTORY***")
  
# TODO 8: If the max offer was greater than the accepted prize, then print out
# the $$$ left out on the table (see the example in this method's header above).
# Otherwise, congratulate the user that they won more than the banker's max
# offer and display the banker's max offer.

"""
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////DO NOT EDIT ANY PORTIONS OF METHODS BELOW///////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
"""
  
"""----------------------------------------------------------------------------
Processes all the code needed to execute a single turn of the game
----------------------------------------------------------------------------"""
def playNextTurn(self):
print("-----------------------------------------------------------------------")
  
# Print out remaining prizes
print("There are " + str(len(self.remainingPrizesBoard)) + " prizes remaining, listed in ascending order: ")
self.printRemainingPrizeBoard()
  
# Display all prize doors
print("\nThe prizes have been dispersed randomly behind the following doors:")
for i in range(0, len(self.remainingPrizesBoard)):
print(str(i), end=" ")
print("")
  
# Print out banker's offer and ask user what to do
print("\nThe banker would like to make you an offer of...................." + '${:,.2f}'.format(self.getBankerOffer()))
print("")      
  
# Get selection from user and choose door
promptStr = "What would you like to do? Enter '-1' to accept the banker's offer, " + "or select one of the doors above (0-" + (str(len(self.remainingPrizesBoard)-1)) + "): "
selectedDoorNum = int(input(promptStr))
if selectedDoorNum >= -1 and selectedDoorNum < len(self.remainingPrizesBoard): # Make sure valid sel.
self.chooseDoor(selectedDoorNum)
else:
print(str(selectedDoorNum) + " is not a valid selection.")
print("")
print("")


'''----------------------------------------------------------------------------
Basically, a getter method for the gameIsOver method. The client
will continually call playNextTurn() until gameIsOver() evaluates
to true.
----------------------------------------------------------------------------'''
def gameIsOver(self):
return self.gameOver


'''----------------------------------------------------------------------------
Copies the constant prizes (from an array) into a temporary array
and uses that array to populate the initial board into the member
variable 'remainingPrizesBoard'.
----------------------------------------------------------------------------'''
def initializeRandomPrizeBoard(self):
# Start with a fresh board with nothing in it
self.remainingPrizesBoard = []
  
# Copies cashPrizes array into prizes ArrayList (a temp ArrayList)
prizes = []
  
for prize in self.cashPrizes:
prizes.append(prize)
  
# Randomizes prizes into remainingPrizesBoard
while len(prizes) > 0:
from random import randint
i = randint(0, len(prizes)-1)
self.remainingPrizesBoard.append(prizes[i]) # Copy into our "board"
del prizes[i]
          
# Debug print which will show the random board contents
#for p in self.remainingPrizesBoard:
# print('${:,.2f}'.format(p), end=" -- ")
#print("")

Solutions

Expert Solution

The code will be (without the TODO comments and only the code which can be edited rest can be copy-pasted below this code)

class CS125_Lab1():
def __init__(self):
# Welcome user to the game
print("Let's play DEAL OR NO DEAL")
  
# Define instance variables and init board
self.cashPrizes = [.01, .50, 1, 5, 10, 50, 100, 250, 500, 1000, 5000, 10000, 100000, 500000, 1000000]
self.remainingPrizesBoard = []
self.gameOver = False
self.offerHistory = []
self.initializeRandomPrizeBoard()


"""----------------------------------------------------------------------------
Prints the locations available to choose from
(0 through numRemainingPrizes-1)
----------------------------------------------------------------------------"""
def printRemainingPrizeBoard(self):
# Copies remaining prizes ArrayList into prizes ArrayList (a temp ArrayList)
prizes = []
for prize in self.remainingPrizesBoard:
prizes.append(prize)
prizes.sort()
for num in prizes:
print('${:,.2f}'.format(num))

"""----------------------------------------------------------------------------
Generates the banks offer. The banker algorithm computes the average
value of the remaining prizes and then offers 85% of the average.
----------------------------------------------------------------------------"""
def getBankerOffer(self):
ans=0
for num in self.remainingPrizesBoard:
ans+=num
if len(self.remainingPrizesBoard)>0:
ans = ans/len(self.remainingPrizesBoard)
return 0.85*ans
  
def chooseDoor(self, door):
self.offerHistory.append(self.getBankerOffer())
if door == -1:
self.gameOver=True
print('The game is over and the banker\'s offer is ',self.getBankerOffer())
self.printOfferHistory(self.getBankerOffer())
else:
print('You selected door '+str(door)+' and the prize behind it was '+str(self.remainingPrizesBoard[door]))
del self.remainingPrizesBoard[door]
if len(self.remainingPrizesBoard) == 1:
self.gameOver=True
print('The prize money is '+str(self.remainingPrizesBoard[0]))
self.printOfferHistory(self.remainingPrizesBoard[0])


def printOfferHistory(self, acceptedPrize):
maxOffer = 0
print("\n\n\n***BANKER HISTORY***")
for i in range(len(self.offerHistory)):
maxOffer=max(maxOffer,self.offerHistory[i])
print('Offer ',i+1,'${:,.2f}'.format(self.offerHistory[i]))

if maxOffer-acceptedPrize>1e-5:
print('left $'+str(maxOffer-acceptedPrize)+' on the table.')
else:
print('Congratulations you won more than the banker\'s max offer - ',maxOffer)

The screenshots are for indentations

The output is

-----------------------------------------------------------------------
There are 15 prizes remaining, listed in ascending order: 
$0.01
$0.50
$1.00
$5.00
$10.00
$50.00
$100.00
$250.00
$500.00
$1,000.00
$5,000.00
$10,000.00
$100,000.00
$500,000.00
$1,000,000.00

The prizes have been dispersed randomly behind the following doors:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 

The banker would like to make you an offer of....................$91,625.27

What would you like to do? Enter '-1' to accept the banker's offer, or select one of the doors above (0-14): 5
You selected door 5 and the prize behind it was 5000

-----------------------------------------------------------------------
There are 14 prizes remaining, listed in ascending order: 
$0.01
$0.50
$1.00
$5.00
$10.00
$50.00
$100.00
$250.00
$500.00
$1,000.00
$10,000.00
$100,000.00
$500,000.00
$1,000,000.00

The prizes have been dispersed randomly behind the following doors:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 

The banker would like to make you an offer of....................$97,866.36

What would you like to do? Enter '-1' to accept the banker's offer, or select one of the doors above (0-13): 6
You selected door 6 and the prize behind it was 1000

-----------------------------------------------------------------------
There are 13 prizes remaining, listed in ascending order: 
$0.01
$0.50
$1.00
$5.00
$10.00
$50.00
$100.00
$250.00
$500.00
$10,000.00
$100,000.00
$500,000.00
$1,000,000.00

The prizes have been dispersed randomly behind the following doors:
0 1 2 3 4 5 6 7 8 9 10 11 12 

The banker would like to make you an offer of....................$105,329.16

What would you like to do? Enter '-1' to accept the banker's offer, or select one of the doors above (0-12): 3
You selected door 3 and the prize behind it was 0.5

-----------------------------------------------------------------------
There are 12 prizes remaining, listed in ascending order: 
$0.01
$1.00
$5.00
$10.00
$50.00
$100.00
$250.00
$500.00
$10,000.00
$100,000.00
$500,000.00
$1,000,000.00

The prizes have been dispersed randomly behind the following doors:
0 1 2 3 4 5 6 7 8 9 10 11 

The banker would like to make you an offer of....................$114,106.55

What would you like to do? Enter '-1' to accept the banker's offer, or select one of the doors above (0-11): 1
You selected door 1 and the prize behind it was 5

-----------------------------------------------------------------------
There are 11 prizes remaining, listed in ascending order: 
$0.01
$1.00
$10.00
$50.00
$100.00
$250.00
$500.00
$10,000.00
$100,000.00
$500,000.00
$1,000,000.00

The prizes have been dispersed randomly behind the following doors:
0 1 2 3 4 5 6 7 8 9 10 

The banker would like to make you an offer of....................$124,479.49

What would you like to do? Enter '-1' to accept the banker's offer, or select one of the doors above (0-10): 7
You selected door 7 and the prize behind it was 10000

-----------------------------------------------------------------------
There are 10 prizes remaining, listed in ascending order: 
$0.01
$1.00
$10.00
$50.00
$100.00
$250.00
$500.00
$100,000.00
$500,000.00
$1,000,000.00

The prizes have been dispersed randomly behind the following doors:
0 1 2 3 4 5 6 7 8 9 

The banker would like to make you an offer of....................$136,077.44

What would you like to do? Enter '-1' to accept the banker's offer, or select one of the doors above (0-9): -1
The game is over and the banker's offer is  136077.43584999998



***BANKER HISTORY***
Offer  1 $91,625.27
Offer  2 $97,866.36
Offer  3 $105,329.16
Offer  4 $114,106.55
Offer  5 $124,479.49
Offer  6 $136,077.44
Congratulations you won more than the banker's max offer -  136077.43584999998

Related Solutions

Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file...
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file must satisfy the following. Define a function named rinsert. This function will accept two arguments, the first a list of items to be sorted and the second an integer value in the range 0 to the length of the list, minus 1. This function shall insert the element corresponding to the second parameter into the presumably sorted list from position 0 to one less...
Computer Science - Java Programming How do you read a text file and store three different...
Computer Science - Java Programming How do you read a text file and store three different pieces of information in the database when the given text file contains this info.: 12345 Computer Science Bob Stone 23456 Art James G. Ocean? These are written in the format as ID Class Name. I was going to take the three different pieces of information by separating them by spaces, but the number of spaces is random and I don't know how to adjust...
DIRECTIONS: Complete the following Programming Exercise in Python. Name the file Lastname_Firstname_E1. You just started your...
DIRECTIONS: Complete the following Programming Exercise in Python. Name the file Lastname_Firstname_E1. You just started your summer internship with ImmunityPlus based in La Crosse, Wisconsin. You are working with the forecasting team to estimate how many doses of an immunization drug will be needed. For each drug estimation, you will be provided the following information: corona.txt 39 20 31 10 42 49 54 21 70 40 47 60 - The size of the target population. - The life expectancy, in...
In this course, you have learned that computer programming is key to computer science. The ability...
In this course, you have learned that computer programming is key to computer science. The ability to program a succession of instructions to resolve a computing problem is a valuable skill that is integral to many aspects of modern life. You have also seen the versatility and distinguishing characteristics that make Python 3 a popular choice for programmers. End-of-Course Essay Write a 1- to 2-page essay in which you connect this course to your own career or personal life. Imagine...
Python Create a Python script file called hw3.py. Ex. 1. Write a program that inputs numbers...
Python Create a Python script file called hw3.py. Ex. 1. Write a program that inputs numbers from the user until they enter a 0 and computes the product of all these numbers and outputs it. Hint: use the example from the slides where we compute the sum of a list of numbers, but initialize the variable holding the product to 1 instead of 0. print("Enter n") n = int(input()) min = n while n != 0: if n < min:...
Assignment Requirements Write a python application that consists of two .py files. One file is a...
Assignment Requirements Write a python application that consists of two .py files. One file is a module that contains functions used by the main program. NOTE: Please name your module file: asgn4_module.py The first function that is defined in the module is named is_field_blank and it receives a string and checks to see whether or not it is blank. If so, it returns True, if not it return false. The second function that is defined in the module is named...
Assignment Content In this course, you have learned that computer programming is key to computer science....
Assignment Content In this course, you have learned that computer programming is key to computer science. The ability to program a succession of instructions to resolve a computing problem is a valuable skill that is integral to many aspects of modern life. You have also seen the versatility and distinguishing characteristics that make Python 3 a popular choice for programmers. End-of-Course Essay Write a 1- to 2-page essay in which you connect this course to your own career or personal...
Write a Python program stored in a file q3.py that: Gets single-digit numbers from the user...
Write a Python program stored in a file q3.py that: Gets single-digit numbers from the user on one line (digits are separated by white space) and adds them to a list. The first digit and the last digit should not be a zero. If the user provides an invalid entry, the program should prompt for a new value. Converts every entry in the list to an integer and prints the list. The digits in the list represent a non-negative integer....
Using Python Shell 2.7, create a file called Assignment6_1.py Create an array based on a basic...
Using Python Shell 2.7, create a file called Assignment6_1.py Create an array based on a basic array Create a for loop that will print the array index the array, print out the index append an element to the array, print out the array insert an element into the array, print out the array pop an element from the array, print out the array remove an element from the array, print out the array reverse the order of the array, print...
Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT