zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    OpenCV:Python3使用OpenCV
    CNN结构:MXNet设计和实现简介
    环的寻找:寻找无向图中所有存在的环-删除点法
    JAVA 构建使用 Native 库
    Android:JAVA使用HDF5存储
    Java:数值-字符串转换(String转Double)
    Qt无法用UTF-8编辑问题
    Boost-QT兼容问题:#define FUSION_HASH #
    JAVA;使用java.awt.Image的不稳定性
    图方法:寻找无向图联通子集的JAVA版本
  • 原文地址:https://www.cnblogs.com/tonony1/p/4794178.html
Copyright © 2011-2022 走看看