Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project — “Guess the number” game

Mini-project description — “Guess the number” game

One of the simplest two-player games is “Guess the number”. The first player thinks of a secret number in some known range while the second player attempts to guess the number. After each guess, the first player answers either “Higher”, “Lower” or “Correct!” depending on whether the secret number is higher, lower or equal to the guess. In this project, you will build a simple interactive program in Python where the computer will take the role of the first player while you play as the second player.

You will interact with your program using an input field and several buttons. For this project, we will ignore the canvas and print the computer‘s responses in the console. Building an initial version of your project that prints information in the console is a development strategy that you should use in later projects as well. Focusing on getting the logic of the program correct before trying to make it display the information in some “nice” way on the canvas usually saves lots of time since debugging logic errors in graphical output can be tricky.

# template for "Guess the number" mini-project
# input will come from buttons and an input field
# all output for the game will be printed in the console
import simplegui
import math
import random

flag = True
# helper function to start and restart the game
def new_game():
    # initialize global variables used in your code here
    global secret_number
    global number_of_guess
    if flag:
        number_of_guess = 7
        secret_number = random.randint(0,99)
        print "New game. Range is [0,100)"
    else:
        number_of_guess = 10
        secret_number = random.randint(0,999)
        print "New game. Range is [0,1000)"

    print "Number of remaining guesses is", number_of_guess
    print

# define event handlers for control panel
def range100():
    # button that changes the range to [0,100) and starts a new game
    global flag
    flag = True

    global secret_number
    secret_number = random.randint(0,99)

    global number_of_guess
    number_of_guess = 7

    print "New game. Range is [0,100)"
    print "Number of remaining guesses is",number_of_guess
    print

def range1000():
    # button that changes the range to [0,1000) and starts a new game
    global flag
    flag = False

    global secret_number
    secret_number = random.randint(0,999)

    global number_of_guess
    number_of_guess = 10

    print "New game. Range is [0,1000)"
    print "Number of remaining guesses is", number_of_guess
    print

def input_guess(guess):
    # main game logic goes here
    global guess_number
    guess_number = int(guess)
    print "Guess was",guess_number

    global number_of_guess
    number_of_guess -= 1
    print "Number of remaining guesses is", number_of_guess

    if  secret_number > guess_number:
        print "Higher!"
        print
    elif secret_number < guess_number:
        print "Lower!"
        print
    else:
        print "Correct!"
        print
        new_game()

    if number_of_guess == 0:
        print "You ran out of guesses.  the number was", secret_number
        print
        new_game()

# create frame
frame = simplegui.create_frame("Guess the number", 200, 200)

# register event handlers for control elements and start frame
frame.add_button("Range is [0,100)", range100, 200)
frame.add_button("Range is [0,1000)", range1000, 200)
frame.add_input("Enter a guess", input_guess, 200)

# call new_game
new_game()

# always remember to check your completed program against the grading rubric
时间: 2024-10-08 01:07:52

Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project — “Guess the number” game的相关文章

【python】An Introduction to Interactive Programming in Python(week two)

This is a note for https://class.coursera.org/interactivepython-005 In week two, I have learned: 1.event-drvien programing 4 event types: Input: button, textbox Keyborad: key up, key down Mouse: click, drag Timer example: # Example of a simple event-

Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project— Rock-paper-scissors-lizard-Spock

Mini-project description — Rock-paper-scissors-lizard-Spock Rock-paper-scissors is a hand game that is played by two people. The players count to three in unison and simultaneously "throw” one of three hand signals that correspond to rock, paper or s

Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project #4 —&quot;Pong&quot;

In this project, we will build a version of Pong, one of the first arcade video games (1972). While Pong is not particularly exciting compared to today's video games, Pong is relatively simple to build and provides a nice opportunity to work on the s

Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project #3 —&quot;Stopwatch: The Game&quot;

Mini-project description - "Stopwatch: The Game" Our mini-project for this week will focus on combining text drawing in the canvas with timers to build a simple digital stopwatch that keeps track of the time in tenths of a second. The stopwatch

Interactive Programming in Python Week7 Code style

1 def draw(c): 2 global paddle1_pos, paddle2_pos 3 4 paddle_width = 80 5 6 if paddle_width/2 <= paddle1_pos + paddle1_vel <= width - paddle_width/2: 7 paddle1_pos += paddle1_vel 8 if paddle_width/2 <= paddle2_pos + paddle2_vel <= width - paddl

Note 2 for &lt;Pratical Programming : An Introduction to Computer Science Using Python 3&gt;

Book Imformation : <Pratical Programming : An Introduction to Computer Science Using Python 3> 2nd Edtion Author : Paul Gries,Jennifer Campbell,Jason Montojo Page : Chapter 2.3 to Chapter 2.5 1.A type consists of two things: (1).a set of values (2).

MTH5001: Introduction to Computer Programming

projectMarch 14, 20191 MTH5001: Introduction to Computer Programming 2018/191.1 Final Report Project: "Networks"1.1.1 Instructions:First, please type your name and student number into the Markdown cell below:Name:Student number:You must write yo

Core Java Volume I — 4.1. Introduction to Object-Oriented Programming

4.1. Introduction to Object-Oriented ProgrammingObject-oriented programming, or OOP for short, is the dominant programming paradigm these days, having replaced the "structured," procedural programming techniques that were developed in the 1970s.

MITx: 6.00.1x Introduction to Computer Science and Programming Using Python Week 2: Simple Programs 4. Functions

ESTIMATED TIME TO COMPLETE: 18 minutes We can use the idea of bisection search to determine if a character is in a string, so long as the string is sorted in alphabetical order. First, test the middle character of a string against the character you'r