![Python Programming: An Introduction to Computer Science, 3rd Ed.](https://www.bartleby.com/isbn_cover_images/9781590282755/9781590282755_largeCoverImage.gif)
Concept explainers
Three Button Monte
Program Plan:
- Import the required packages.
- Declare a gameplay function. Inside the function,
- Create the application window using the “button.py” module.
- Activate the doors.
- Get the random number.
- Assign the values to “false”.
- Check the condition.
- Assign “point1” to “true”.
- Check the condition.
- Assign the “point2” to “true”.
- Otherwise, Assign the “point3” to “true”
- Activate all the three doors.
- Get the action mouse clicked.
- Check the condition using “while” loop.
- Check the condition for selecting the door 1 to be clicked.
- Check the condition for selecting the door 2 to be clicked.
- Check the condition for selecting the door 3 to be clicked.
- Definition of “printSummary()”.
- Print the result.
- Definition of main function.
- Creating the application window by setting title, cords and background.
- Define “Quit” button and set to active.
- Assign the text to the interface.
- Call the method draw.
- Initialize variables to enter loop
- Check the condition for “Quit” not clicked.
- Call the method “gameplay()”
- Call the method “printSummary()”
- Call the main function.
![Check Mark](/static/check-mark.png)
Explanation of Solution
Program:
Refer the program “button.py” given in the “Chapter 10” from the text book. Add the method “update()” along with the given code.
#Define the method update
def update(self, win, label):
#Call the method undraw()
self.label.undraw()
#Assign the position to centre
center = self.center
#Assign the label
self.label = Text(center, label)
#Set active to false
self.active = False
#Call the method draw()
self.label.draw(win)
Main.py:
#Import required packages
from button import Button
from graphics import GraphWin, Point, Text
from random import random
from time import sleep
#Definition of gameplay method
def gameplay(click, win, wins, losses):
#use button.py module to create doors and activate them
door1 = Button(win, Point(-7.5, -3), 5, 6, "Door 1")
door2 = Button(win, Point(0, -3), 5, 6, "Door 2")
door3 = Button(win, Point(7.5, -3), 5, 6, "Door 3")
#Activate the doors
door1.activate()
door2.activate()
door3.activate()
#Get the random number
x = random() * 3
#Assign the values to false
point1 = point2 = point3 = False
#check the condition
if 0 <= x <1:
#Assign point1 to True
point1 = True
#Check the condition
elif 1 <= x < 2:
#Assign the point2 to True
point2 = True
#Otherwise
else:
#Assign the point2 to True
point3 = True
#Get the action mouse click
click = win.getMouse()
#Check the condition using "while" loop
while door1.clicked(click) or door2.clicked(click) or door3.clicked(click):
#If door1 is clicked
if door1.clicked(click):
#Assign point1 to true
if point1 == True:
#Update the interface
door1.update(win,"Victory!")
#Call the method sleep
sleep(1)
#Increment the value of wins
wins = wins + 1
#Otherwise
else:
#Update the interface
door1.update(win,"Try Again")
#Call the method sleep
sleep(1)
#Increment the losses
losses = losses + 1
break
#Check the door2 is clicked
elif door2.clicked(click):
#Assign point2 to true
if point2 == True:
#Update the interface
door2.update(win,"Victory!")
#Call the method sleep
sleep(1)
#Increment the value of wins
wins = wins + 1
#Otherwise
else:
#Update the interface
door2.update(win,"Try Again")
#Call the method sleep
sleep(1)
#Increment the value of losses
losses = losses + 1
break
else:
if point3 == True:
door3.update(win,"Victory!")
#Call the method sleep
sleep(1)
#Increment the value of wins
wins = wins + 1
#Otherwise
else:
#Update the interface
door3.update(win,"Try Again")
#Call the method sleep
sleep(1)
#Increment the value of losses
losses = losses + 1
break
#Return the values
return click, wins, losses
#Definition of printSummary
def printSummary(wins, losses):
#Print the result
print("Wins: {0:5} Losses: {1:5}".format(wins, losses))
#Definition of main
def main():
#Creating the application window by setting title, cords and background
win = GraphWin("Three Button Monte", 500, 300)
win.setCoords(-12, -12, 12, 12)
win.setBackground("green3")
#define Quit button and set to active
gameover = Button(win, Point(9, 10), 3, 3, "Quit")
gameover.activate()
#Assign the text to the interface
direction = Text(Point(0, 10), "Pick a Door")
#Call the method draw
direction.draw(win)
#initialize variables to enter loop
click = Point(0,0)
wins = losses = 0
#Check the condition
while not gameover.clicked(click):
#Call the method
click, wins, losses = gameplay(click, win, wins, losses)
#Call the method printSummary
printSummary(wins, losses)
#Call the main function
main()
Output:
Screenshot of output
Clicking the “Exit” button:
Clicking the “Door1”, “Door2”, “Door3” will displays the opened door leads to “Victory!” or “Try Again”. After clicking the “Exit” button it prints the count of the corresponding result.
Wins: 1 Losses: 2
Want to see more full solutions like this?
Chapter 10 Solutions
Python Programming: An Introduction to Computer Science, 3rd Ed.
- As described in Learning from Mistakes, the failure of the A380 to reach its sales goals was due to Multiple Choice: a) misunderstanding of supplier demands. b) good selection of hotel in the sky amenities. c) changes in customer demands. d) lack of production capacity.arrow_forwardNumerous equally balanced competitors selling products that lack differentiation in a slow growth industry are most likely to experience high: a) intensity of rivalry among competitors. b) threat of substitute products. c) threat of new entrants. d) bargaining power of suppliers.arrow_forwardA Dia file has been created for you to extend and can be found on Company.dia represents a completed ER schema which, models some of the information implemented in the system, as a starting point for this exercise. Understanding the ER schema for the Company database. To demonstrate that you understand the information represented by the schema, explain using EMPLOYEE, DEPARTMENT, PROJECT and DEPENDENT as examples: attributes, entities and relationships cardinality & participation constraints on relationships You should explain questions a and b using the schema you have been given to more easily explain your answers. Creating and Extending Entity Relationship (EER) Diagrams. To demonstrate you can create entity relationship diagrams extend the ER as described in Company.dia by modelling new requirements as follows: Create subclasses to extend Employee. The employee type may be distinguished further based on the job type (SECRETARY, ENGINEER, MANAGER, and TECHNICIAN) and based…arrow_forward
- Computer programs can be very complex, containing thousands (or millions) of lines of code and performing millions of operations per second. Given this, how can we possibly know that a particular computer program's results are correct? Do some research on this topic then think carefully about your response. Also, explain how YOU would approach testing a large problem. Your answer must be thoughtful and give some insight into why you believe your steps would be helpful when testing a large program.arrow_forwardCould you fix this? My marker has commented, What's missing? The input list is the link below. https://gmierzwinski.github.io/bishops/cs321/resources/CS321_Assignment_1_Input.txt result.put(true, dishwasherSum); result.put(false, sinkSum); return result; }}arrow_forwardPLEG136: Week 5 Portofolio Project Motion to Compelarrow_forward
- B A E H Figure 1 K Questions 1. List the shortest paths between all node pairs. Indicate the number of shortest paths that pass through each edge. Explain how this information helps determine edge betweenness. 2. Compute the edge betweenness for each configuration of DFS. 3. Remove the edge(s) with the highest betweenness and redraw the graph. Recompute the edge betweenness centrality for the new graph. Explain how the network structure changes after removing the edge. 4. Iteratively remove edges until at least two communities form. Provide step-by-step calculations for each removal. Explain how edge betweenness changes dynamically during the process. 5. How many communities do you detect in the final step? Compare the detected communities with the original graph structure. Discuss whether the Girvan- Newman algorithm successfully captures meaningful subgroups. 6. If you were to use degree centrality instead of edge betweenness for community detection, how would the results change?arrow_forwardUnit 1 Assignment 1 – Loops and Methods (25 points) Task: You are working for Kean University and given the task of building an Email Registration System. Your objective is to generate a Kean email ID and temporary password for every new user. The system will prompt for user information and generate corresponding credentials. You will develop a complete Java program that consists of the following modules: Instructions: 1. Main Method: ○ The main method should include a loop (of your choice) that asks for input from five users. For each user, you will prompt for their first name and last name and generate the email and password by calling two separate methods. Example о Enter your first name: Joe Enter your last name: Rowling 2.generateEmail() Method: This method will take the user's first and last name as parameters and return the corresponding Kean University email address. The format of the email is: • First letter of the first name (lowercase) + Full last name (lowercase) +…arrow_forwardI have attached my code, under I want you to show me how to enhance it and make it more cooler and better in graphics with following the instructions.arrow_forward
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageProgramming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage Learning
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTEBK JAVA PROGRAMMINGComputer ScienceISBN:9781305480537Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337102124/9781337102124_smallCoverImage.gif)
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
![Text book image](https://www.bartleby.com/isbn_cover_images/9781305480537/9781305480537_smallCoverImage.jpg)
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337102100/9781337102100_smallCoverImage.gif)