“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, 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
“Guess the number” game的更多相关文章
- JavaScript Math和Number对象
目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...
- Harmonic Number(调和级数+欧拉常数)
题意:求f(n)=1/1+1/2+1/3+1/4-1/n (1 ≤ n ≤ 108).,精确到10-8 (原题在文末) 知识点: 调和级数(即f(n))至今没有一个完全正确的公式, ...
- Java 特定规则排序-LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Eclipse "Unable to install breakpoint due to missing line number attributes..."
Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...
- 移除HTML5 input在type="number"时的上下小箭头
/*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...
- iOS---The maximum number of apps for free development profiles has been reached.
真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...
- 有理数的稠密性(The rational points are dense on the number axis.)
每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.
- [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- [LeetCode] Number of Boomerangs 回旋镖的数量
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- [LeetCode] Number of Segments in a String 字符串中的分段数量
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...
随机推荐
- canvas-star6.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 百度面试题:从输入url到显示网页,后台发生了什么?
参考http://igoro.com/archive/what-really-happens-when-you-navigate-to-a-url/ http://www.cnblogs.com/we ...
- FPGA中的时序分析(四)
常用约束语句说明 关于Fmax 上述是实现Fmax的计算公式,clock skew delay的计算如下图, 就是两个时钟的差值.到头来,影响Fmax的值的大小就是组合逻辑,而Fmax是针对 ...
- 使用AS3.0代码实现给图片添加滤镜的模糊与斜角效果
滤镜可应用于任何显示对象(即,从 DisplayObject 类继承的对象), 例如 MovieClip.SimpleButton.TextField 和 Video 对象,以及 BitmapData ...
- windows7 下的日期没有internet时间的选项卡
原因1:你在某个域里面,退出就可以了 原因2:你把windows Time的服务给禁掉或者关掉了, 步骤:运行,输入services.msc 确定,查找到windows Time开了即可
- linux取某个字段排重
排重统计 cat a.txt | awk -F ';' '{print $2}' | sort -u | wc -l
- SQL语句基础知识
1.关于SQL语句中exists与not exists的问题 course表如下:课程代号 课程名称 K01 奥运会主题曲 K02 喜爱的专辑 K03 VB技术大全 K04 经典歌曲 K05 个人单曲 ...
- 学习总结 java连接数据库
package com.hanqi.test; import java.sql.*; public class jdbcTest { public static void main(String[] ...
- WinDbg调试CPU占用高的问题 试验+实战 《第七篇》
一.High CPU试验 1.示例代码 static void Main(string[] args) { Console.Clear(); Console.WriteLine("到命令行下 ...
- IE CSS HACK
IE 属性值 HACK .test { background:blue; /*所有浏览器*/ background:yellow\9; /*所有IE浏览器*/ background:green\0; ...