Three Button Monte
Program Plan:
- Import the required packages.
- Declare a main function. Inside the main function,
- Create the application window.
- Draw the interface widget by creating three buttons.
- Assign the text to the interface.
- Call the method “draw()”.
- 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.
- Call the “getMouse()” function
- Call the main function.
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 the required packages
from button import Button
from graphics import GraphWin, Point, Text
from random import random
#Definition of main method
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")
#Draw the interface widget by creating three button
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")
#Assign the text to the interface
direction = Text(Point(0, 10), "Pick a Door")
#Call the method draw
direction.draw(win)
#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
#Chcek the condition
elif 1 <= x < 2:
#Assign the point2 to True
point2 = True
#Otherwise
else:
#Assign the point3 to True
point3 = True
#Activate all the three doors
door1.activate()
door2.activate()
door3.activate()
#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!")
else:
#If door1 is clicked
if door2.clicked(click):
#Update the interface
door2.update(win,"Correct Door.")
#Get the action mouse click
click = win.getMouse()
else:
#Update the interface
door3.update(win,"Correct door.")
#Get the action mouse click
click = win.getMouse()
#If door2 is clicked
elif door2.clicked(click):
#Assign point2 to true
if point2 == True:
#Update the interface
door2.update(win,"Victory!")
else:
#If door2 is clicked
if door3.clicked(click):
#Update the interface
door3.update(win,"Correct Door.")
#Get the action mouse click
click = win.getMouse()
else:
#Update the interface
door1.update(win,"Correct door.")
#Get the action mouse click
click = win.getMouse()
else:
#Assign point3 to true
if point3 == True:
#Update the interface
door3.update(win,"Victory!")
else:
#If door2 is clicked
if door2.clicked(click):
#Update the interface
door2.update(win,"Correct Door.")
#Get the action mouse click
click = win.getMouse()
else:
#Update the interface
door1.update(win,"Correct door.")
#Get the action mouse click
click = win.getMouse()
#Call the getMouse()
win.getMouse()
#Call the main function
main()
Output:
Screenshot of output
Screenshot of output
Want to see more full solutions like this?
Chapter 10 Solutions
Python Programming: An Introduction to Computer Science, 3rd Ed.
- You are going to write a simulation for a mini survival game. In this game, one flyand three frogs are placed randomly on a board with the size 7x7.In each iteration, they move randomlywithin the board, and the simulation stops when one of the frogs eats the fly.Fly and frogs are essentially creatures that can move, make noise, and eat. Frogs can move up to 2squares in any direction and flies can move up to 1. Frogs make the "Croak! Croak!" sound, and fliesmake "ZzzZZz!". Since frogs should not eat their kind, a function of "isEatable" should also beimplemented in decision making. A variable or function to check if the fly is alive is also required as aterminate condition for simulation.In each iteration, an 'f' character represents frogs' position on board, and an '*' character is used torepresent the position of fly. Use the topics (Abstract classes, Interface, etc.) to implementthe simulation. (in java) An example output is given below.arrow_forwardLangauge is C.arrow_forwardIn the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper, or scissors. If both players choose the same option, then the result is a tie. However, if they choose differently, the winner is determined as follows: Rock beats scissors, because a rock can break a pair of scissors. Scissors beats paper, because scissors can cut paper. Paper beats rock, because a piece of paper can cover a rock. Create a game in which the computer randomly chooses rock, paper, or scissors. Let the user enter a number 1, 2, or 3, each representing one of the three choices. Then, determine the winner. Program should validate all user input Game should ask the user to play again and continue if yes and stop if no. Once the user stops playing, program should print the total number of wins.arrow_forward
- In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper, or scissors. If both players choose the same option, then the result is a tie. However, if they choose differently, the winner is determined as follows:Rock beats scissors, because a rock can break a pair of scissors.Scissors beats paper, because scissors can cut paper.Paper beats rock, because a piece of paper can cover a rock.Create a game in which the computer randomly chooses rock, paper, or scissors.Let the user enter a number 1, 2, or 3, each representing one of the three choices.Then, determine the winner.Program should validate all user inputGame should ask the user to play again and continue if yes and stop if no.Once the user stops playing, program should print the total number of wins.arrow_forwardWrite a program that displays twocircles with radius 10 at location (40, 40) and (120, 150) with a line connectingthe two circles, as shown in Figure . The distance between the circlesis displayed along the line. The user can drag a circle. When that happens, thecircle and its line are moved, and the distance between the circles is updated.arrow_forwardWrite a program that plays a dice game called "21" It is a variation on BlackJack where one player plays against the computer trying to get 21 or as close to 21 without going over. Here are the rules of the game: You will play with dice that have numbers from 1 to 11. To win, the player or the computer has to get to 21, or as close as possible without going over. If the player or computer goes over 21, they instantly lose. If there is a tie, the computer wins. Starting the game: The player is asked to give the computer a name. For now, we'll simply call the computer opponent, "computer." The game starts with rolling four dice. The first two dice are for the player. These two dice are added up and the total outputted to the screen. The other two dice are for the computer. Likewise, their total is outputted to the screen. Player: If a total of 21 has been reached by either the player or the computer, the game instantly stops and the winner is declared. Otherwise,…arrow_forward
- Write a program that plays a dice game called "21" It is a variation on BlackJack where one player plays against the computer trying to get 21 or as close to 21 without going over. Here are the rules of the game: You will play with dice that have numbers from 1 to 11. To win, the player or the computer has to get to 21, or as close as possible without going over. If the player or computer goes over 21, they instantly lose. If there is a tie, the computer wins. Starting the game: The player is asked to give the computer a name. For now, we'll simply call the computer opponent, "computer." The game starts with rolling four dice. The first two dice are for the player. These two dice are added up and the total outputted to the screen. The other two dice are for the computer. Likewise, their total is outputted to the screen. Player: If a total of 21 has been reached by either the player or the computer, the game instantly stops and the winner is declared. Otherwise,…arrow_forwardWrite a program that allows a user to play HiLo game. User wins if he/she can guess the number between 1 and 100 within at most 6 iterations. If s/he guesses a number greater than the secret number. The message shown is "It is Higher than secret shown" If s/he guesses a number lower than the secret number. The message shown is “ It is Lower than secret" If the guessed number equal to the secret number. The message shown is "Cong! you won" If the user consumed the 6 trails without finding the secret number a message "Sorry! Try again" will be shown before termination. : Output - CppApplication_1 (Run) #3 O Enter a positive integer seed value: 100 Enter your guess : 40 It is Higher than secret Enter your guess : 20 It is Higher than secret Enter your guess : 10 It is Lower than secret Enter your guess : 15 It is Lower than secret Enter your guess : 17 It is Lower than secret Enter your guess : 18 Cong! you won PE Outputarrow_forwardYou are a Chemistry major who has to create a program for first time chemistry lab users. The Erlenmeyer flask contains a nitric acid solution. When the student presses a button, 0.5 mL of NaOH solution can be added to a buret, which is set up over the Erlenmeyer flask. An indicator is added to the solution being titrated. The indicator is a substance that changes to blue when the reaction is complete (endpoint). You determine the amount of mixture that will cause this reaction to occur. Create a titration simulator that will allow a student to add up to 18 mL of titrant. Notify the student when the mixture turns blue. In Python please :)arrow_forward
- provide your own code, pleasearrow_forwardA class plays a fishing game in which a large tub is filled with plastic fish that are identical, except that some are red and the rest are white. A student is blindfolded and pulls 10 fish out of the tub. The student removes the blindfold, writes down how many of each color fish she got, and then puts the fish back in the tub. Each student takes a turn. The results are shown in the table. Display these data in a dot plot. Write and answer (to the extent possible) at least three questions about the data display in part (a); include at least one question at each of the three graph-reading levels discussed in the reading.arrow_forwardJavaStudents will be creating a simple trivia game for two players. The program will work like the following: Starting with player 1, each player gets a turn answering 5 trivia questions (There are 10 questions, 5 for each player). When a question is displayed, four possible answers are also displayed. Only one of the answers is correct, and if the player selects the correct answer, he or she earns a point.After answers have been selected for all of the questions, the program displays the number of points earned by each player and declares the player with the highest number of points the winner.You are to design a Question class to hold the data for a trivia question. The Question class should have String fields for the following data: A trivia questionPossible answer 1Possible answer 2Possible answer 3Possible answer 4The number of the correct answer (1,2,3, or 4)The Question class should have appropriate constructor(s), accessor, and mutator methods. The program should create an…arrow_forward
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage