Python Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science
3rd Edition
ISBN: 9781590282779
Author: John Zelle
Publisher: Franklin Beedle & Associates
bartleby

Concept explainers

Question
Book Icon
Chapter 10, Problem 4PE
Expert Solution & Answer
Check Mark
Program Plan Intro

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.

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()

Sample Output

Output:

Screenshot of output

Python Programming: An Introduction to Computer Science, Chapter 10, Problem 4PE

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?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
CPS 2390 Extra Credit Assignment For each problem, choose the best answer and explain how you arrived at your answer. (15 points each.) 1.If control is redirected to location x4444 after the execution of the following instructions, what should have been the relationship between R1 and R2 before these instructions were executed? Address Instruction x4400 1001100010111111 x4401 0001100100100001 x4402 0001100001000100 x4403 0000100001000000 A. R1 R2 (R1 was greater than R2) B. R1 R2 (R2 was greater than R1) C. R1 R2 (R1 and R2 were equal) = D. Cannot be determined with the given information. 2. If the value stored in RO is 5 at the end of the execution of the following instructions, what can be inferred about R5? Address x3000 Instruction 0101000000100000 x3001 0101111111100000 x3002 0001110111100001 x3003 0101100101000110 x3004 0000010000000001 x3005 0001000000100001 x3006 0001110110000110 x3007 0001111111100001 x3008 0001001111111000 x3009 0000100111111000 x300A 0101111111100000 A. The…
Need help writing code to answer this question in Python! (image attached)
Need help with python code! How do I simplify my code for a beginner to understand, simple fixed format and centering? Such as:  print(f"As an int variable: {age_int:^7}") print(f"In numeric binary: {age_int:^7b}") My Code:name = input("Enter your name: ")print(f"In text name is: {' '.join(name)}")decimal_values = []binary_values = []for letter in name:   ascii_val = ord(letter)   binary_val = format(ascii_val, '08b')   decimal_values.append(str(ascii_val))   binary_values.append(binary_val)# Loop through each letter:print(f"In ASCII decimal: {' '.join(decimal_values)}")print(f"In ASCII binary: {' '.join(binary_values)}")# Ageage_str = input("Enter your age: ")age_int = int(age_str)print(f"As a string \"{age_str}\": {' '.join(age_str)}")age_decimal_values = []age_binary_values = []for digit in age_str:   ascii_val = ord(digit)   binary_val = format(ascii_val, '07b')   age_decimal_values.append(str(ascii_val))   age_binary_values.append(binary_val)print(f"In ASCII decimal: {'…
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
CMPTR
Computer Science
ISBN:9781337681872
Author:PINARD
Publisher:Cengage
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781305480537
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,