• 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
  • 相关阅读:
    LeetCode 811. Subdomain Visit Count (子域名访问计数)
    LeetCode 884. Uncommon Words from Two Sentences (两句话中的不常见单词)
    LeetCode 939. Minimum Area Rectangle (最小面积矩形)
    LeetCode 781. Rabbits in Forest (森林中的兔子)
    LeetCode 739. Daily Temperatures (每日温度)
    三种方式实现按钮的点击事件
    239. Sliding Window Maximum
    14.TCP的坚持定时器和保活定时器
    13.TCP的超时与重传
    12.TCP的成块数据流
  • 原文地址:https://www.cnblogs.com/tonony1/p/4794178.html
Copyright © 2020-2023  润新知