码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-09-09 13:13:40      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

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

 

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

标签:

原文地址:http://www.cnblogs.com/tonony1/p/4794178.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!