项目描述: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

“Guess the number” game的更多相关文章

  1. JavaScript Math和Number对象

    目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...

  2. Harmonic Number(调和级数+欧拉常数)

    题意:求f(n)=1/1+1/2+1/3+1/4-1/n   (1 ≤ n ≤ 108).,精确到10-8    (原题在文末) 知识点:      调和级数(即f(n))至今没有一个完全正确的公式, ...

  3. Java 特定规则排序-LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  4. Eclipse "Unable to install breakpoint due to missing line number attributes..."

    Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...

  5. 移除HTML5 input在type="number"时的上下小箭头

    /*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...

  6. 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.免费应用程序调试最 ...

  7. 有理数的稠密性(The rational points are dense on the number axis.)

    每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.

  8. [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  9. [LeetCode] Number of Boomerangs 回旋镖的数量

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  10. [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 ...

随机推荐

  1. 自己的3dmax作品RX-105柯西高达

    背后视角带导弹仓. 侧面. 侧后视角. 前侧视角. 斜前上视角 使用关联实现骨骼功能,头.躯干.肩.上臂.前臂.手腕.手指.腰.髋关节.踝关节.脚掌皆由骨骼(是通过多边形关联实现骨骼功能,而不是使用3 ...

  2. 如何在使用 RemoteWebDriver 打开网页的同时获取 Http 状态码

    最近一直在用Selenium这个开源项目写一些web 自动化的小玩意.本来一直运行的挺好,直到有一天突然发现资源抓取失败了,翻看日志才发现,原来本该正常打开的页面返回了504错误所以自然失败了.如何避 ...

  3. Flex AdvancedDatagrid使用

    首先我先来看下利用Advanced Datagrid做出的效果,然后我们再对其中所利用的知识进行讲解,效果图如下: 我们来看下这个效果我们所用到的关于Advanced Datagrid的相关知识: 一 ...

  4. javaScript 封装

    在基于web 的b/s 架构的项目中, 丰富的界面都离不开 javascript, javascript 在 html 中变得越来越强大,但是我们在写 javascript 的时候都比较随意,随着页面 ...

  5. SQL函数——LENGTH()和LENGTHB()

    LENGTH()函数是比较简单同时也是非常有用的一个函数,在此小记一下,加深印象以备后用! 1:先上实验的例子——我新建了一张Student表,插入了若干条测试数据,如下图所示: 2:实验LENGTH ...

  6. Eclipse 安装反编译插件jadclipse

    下载jadClipse地址: 链接: http://pan.baidu.com/s/1kTN4TPd  提取码: 3fvd 将net.sf.jadclipse_3.3.0.jar拷贝到eclipse的 ...

  7. 华为OJ平台——放苹果(典型整数划分问题)

    题目描述: 输入m,n,分别表示苹果数与盘子的总数,要求输出苹果放在n个盘子的方法总数(注意511和151是一种情况),例如输入 7 3 输出8((7),(6,1),(5,2),(4,3),(5,1, ...

  8. ASP.NET的SEO:目录

    ASP.NET的SEO:基础知识 ASP.NET的SEO:Global.asax和HttpModule中的RewritePath()方法--友好的URL ASP.NET的SEO:正则表达式 ASP.N ...

  9. 网站屏蔽指定ip

    修改.htaccess文件 Order Deny,Allow     //开启屏蔽Deny from 124.64.242.117  //要屏蔽的ip

  10. js动态改变图片热区坐标,手机端图片热区自适应

    <img id='banner1' src="images/banner.jpg" usemap="#banner" border="0&quo ...