“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 ...
随机推荐
- Windows环境Mycat数据库分库分表中间件部署
下载地址MYCAT官方网站 jdk安装配置 首先去oracle官网下载并安装jdk8,添加环境变量,JAVA_HOME设置为D:\Worksoftware\Java\jdk1.8 CLASSPATH设 ...
- 解决wamp mysql数据库出现乱码的问题。
一般的乱码情况: 如果在控制台上出现這样的乱码,一般在phpmysqladmin上也会出现乱码,因为他们都一样 一个在控制台出现,一个在页面出现. 首先在mysql.exe上输出 mysql>S ...
- yii中rights安装
具体安装程序可参考:http://www.focalhot.com/blog/5.html 我在安装过程中一直存在问题,提示表auth_assignment不存在 由于我是将项目中已有的代码复制过来, ...
- CODEVS 1036 商务旅行
题目描述 Description 某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间. 假设有N个城镇,首都编号为1,商人从首都出发,其他各城镇之间都有道路连接,任 ...
- 学习资料 经典SQL语句大全
一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...
- C#之泛型
泛型是C# 2.0版本才有的语言特性,是具有参数类型占位符的类.结构.接口和方法.这些占位符是类.结构.接口和方法所存储或使用的一个或多个占位符.简单来说,就是变量类型的参数化. 以下是详细demo: ...
- java基础回顾(五)——Stack、Heap
栈(stack):是简单的数据结构,但在计算机中使用广泛.栈最显著的特征是:LIFO(Last In, First Out,后进先出).比如我们往箱子里面放衣服,先放入的在最下方,只有拿出后来放入的才 ...
- Vmware ESX5i 环境下部署Windows Storage Server 2008 R2
ESX5i 环境下部署Windows Storage Server 2008 R2 Windows Storage Server 2008 这款产品微软早已发布,WSS2008是基于Win ...
- pyhton标准库 json
使用loads方法即可将json字符串转换成python对象,对应关系如下: #JSON Python object dict array list string unicode number (in ...
- ibatis 存储过程写法
<?xml version=) ORDER BY a.DepId) AS row_n,a.DepId,a.DepName,a.ParentDepId,a.DepCode,a.CustomerS ...