“Guess the number” game

项目描述:https://class.coursera.org/interactivepython-004/human_grading/view/courses/972072/assessments/29/submissions

    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, random, math


# initialize global variables used in your code
frame_width = 100
frame_height = 200
button_width = 100
input_width = 100
num_range_low = 0
num_range_high = 100
player_num = 0
player_guess_times = 0

# helper function to start and restart the game
def new_game():
    global player_num, player_guess_times   
    player_num = random.randrange(num_range_low, num_range_high)
    player_guess_times = int(math.ceil(math.log(num_range_high - num_range_low + 1, 2)))
    print ''
    print 'New game start, number range is [', num_range_low, ',', num_range_high, ').'
    print 'You have ', player_guess_times, ' chances to guess.'

 
# define event handlers for control panel
def range100():
    # button that changes range to range [0,100) and restarts
    global num_range_high
    num_range_high = 100
    new_game()

def range1000():
    # button that changes range to range [0,1000) and restarts
    global num_range_high
    num_range_high = 1000
    new_game()
    
def input_guess(guess):
    # main game logic goes here	
    global player_guess_times
    player_guess_times -= 1
    guess_num = int(guess)
    
    print 'You guessed: ', guess_num, '. ',
    print 'Your remained ', player_guess_times, 'guess chances.',
    
    if guess_num == player_num:
        print 'Correct!'
        new_game()
    elif guess_num > player_num:
        print 'Higher'
    else:
        print 'Lower'
    
    if player_guess_times <= 0:
        print 'You lose!'
        new_game()

    
# create frame
frame = simplegui.create_frame('Guess the number', frame_width, frame_height)



# register event handlers for control elements
frame.add_button('range100', range100, button_width)
frame.add_button('range1000', range1000, button_width)
frame.add_input('guess number', input_guess, input_width)


# call new_game and start frame
new_game()
frame.start()


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


演示地址: http://www.codeskulptor.org/#user29_oKHzERABsF_0.py



文/闫鑫原创   转载请注明出处http://blog.csdn.net/yxstars/article/details/23289283


转载于:https://www.cnblogs.com/iplus/p/4467184.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import random import datetime class GuessNumber: def __init__(self): self.history = [] self.best_score = None self.current_score = None def start_game(self): self.current_score = 0 target_number = random.randint(1, 100) print("猜数字游戏开始!") while True: guess = input("请输入一个1到100之间的整数:") self.current_score += 1 if not guess.isdigit(): print("输入的不是整数,请重新输入!") continue guess_number = int(guess) if guess_number < 1 or guess_number > 100: print("输入的数字不在1到100之间,请重新输入!") continue if guess_number == target_number: print("恭喜你,猜对了!") print("你一共猜了{}次".format(self.current_score)) self.history.append((datetime.datetime.now(), self.current_score)) if self.best_score is None or self.current_score < self.best_score: self.best_score = self.current_score print("你创造了新纪录!") return elif guess_number > target_number: print("猜大了,请继续猜!") else: print("猜小了,请继续猜!") def show_history(self): print("游戏历史记录:") for game in self.history: print("日期:{},成绩:{}".format(game[0], game[1])) def show_score(self): print("当前成绩:{}".format(self.current_score)) if self.best_score is not None: print("历史最好成绩:{}".format(self.best_score)) class Game: game = GuessNumber() while True: game.start_game() game.show_score() choice = input("是否开始新游戏?(输入 yes 或者 no)") if choice.lower() == "no": game.show_history() break
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值