Coursera-an Introduction to Interactive programming in Python (Part 1)-mini-project-"Guess the number" game

Source: Internet
Author: User


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 I s higher, lower or equal to the guess. In this project, you'll build a simple interactive program in Python where the computer would take the role of the first Player while you play as the second player.



You'll interact with the your program using an input field and several buttons. For this project, we'll ignore the canvas and print the computer ' s responses in the console. Building an initial version of your project, prints information in the console is a development strategy so you Shou LD use is later projects as well. Focusing on getting the logic of the program correct before trying to make it display the information in some ' nice ' by the O n the canvas usually saves lots of time since debugging logic errors in graphical output can is 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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.