• About Me

    Lily@Melbourne, Australia


  • Follow 静水深流 || Still waters run deep on WordPress.com
  • Archives

  • Advertisements

Python-Mini-project (Week2)

这个程序的内在逻辑一点都不难,因为它差不多和“Hello, world!”一样知名,是学习一门程序语言的必修项目。可是加上图形的输入框,就得费点劲了。

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.

Mini-project development process

We have provided a basic template for this mini-project here . Our suggested development strategy for the basic version of “Guess the number” is:

  1. Decide on a set of global variables that contain the state of the game. For example, one obvious choice is the secret number that has been generated by the program. You will need other global variables, especially to accommodate later extensions to the basic game.
  2. Figure out how to generate a random secret number in a given range, low to high. When discussing ranges, we will follow the standard Python convention of including the low end of the range and excluding the high end of the range, which can be expressed mathematically as [low, high). So, [0, 3) means all of the numbers starting at 0 up to, but not including 3. In other words 0, 1, and 2. We suggest using the range [0, 100) in your first implementation. Hint: look at the functions in the random module to figure out how to easily select such a random number. We suggest testing this in a separate CodeSkulptor tab before adding code to your project.
  3. Figure out how to create an input text box using the simplegui module. You will use this input to get the guess from the user. For all variants of the game, this input field should always be active (in other words, a game should always be in progress). Again, test in a separate CodeSkulptor tab before adding code to your project.
  4. Write the event handler input_guess(guess) that takes the input guess, compares it to the secret number and prints out the appropriate response. Remember that guess is a string so you will need to convert it into a number before testing it against the secret number. Hint: We have showed you how to convert strings to numbers in the lectures.
  5. Test your code by playing multiple games of “Guess the number” with a fixed range. At this point, you will need to re-run your program between each game (using the CodeSkulptor “Run” button).
  6. Fill in your new_game() function so the generation of the secret number is now done inside this function. That is, calling new_game()should compute a random secret number and assign it to a global variable. You can now call the function new_game() in the body of your code right before you start your frame.



# 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 random
import math
# initialize global variables used in your code
cmp_number = 0
choose_range = 100
max_count = 7
count = 0
# helper function to start and restart the game
def new_game():
global cmp_number, choose_range,count
cmp_number = random.randrange(0, choose_range)
count = 0
print “”
print “New game, Range is from 0 to”, choose_range
print “Number of remaining guesses is”, max_count
print “”
# define event handlers for control panel
def range100():
# button that changes range to range [0,100) and restarts
global choose_range, max_count
choose_range = 100
max_count = int(math.ceil(math.log(choose_range, 2)))

def range1000():
# button that changes range to range [0,1000) and restarts
global choose_range, max_count
choose_range = 1000
max_count = int(math.ceil(math.log(choose_range, 2)))

def input_guess(guess):
# main game logic goes here
global max_count, count
int_guess = int(guess)
print “Guess was” , int_guess
count = count + 1
rem_count = max_count – count
if count < max_count and int_guess != cmp_number:
if int_guess > cmp_number:
print “Number of remaining guesses is ” , max_count – count
print “Lower!”
print “Number of remaining guesses is ” , max_count – count
print “Higher!”
elif count <= max_count and int_guess == cmp_number:
print “Correct! The number was”, cmp_number
print “And it only took you”, count, “tries!”
print “Number of remaining guesses is ” , max_count – count
print “You ran out of guesses. The number was”, cmp_number

print “”

# create frame

frame = simplegui.create_frame(“Guess the number”, 200, 200)
# register event handlers for control elements

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 and start frame


# always remember to check your completed program against the grading rubric



Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: