项目描述: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. MPush开源消息推送系统:简洁、安全、支持集群

    引言由于之前自己团队需要一个消息推送系统来替换JPUSH,一直找了很久基本没有真正可用的开源系统所有就直接造了个轮子,造轮子的时候就奔着开源做打算的,只是后来创业项目失败一直没时间整理这一套代码,最近 ...

  2. Flash图表控件FusionCharts如何自定义图表上的垂直线

    什么是垂直分割线 垂直(或条形图中的水平)分隔线是用来帮助用户分隔数据块的.可以被放置在任何两个数据点,即使是不规则的间隔也可以. <chart caption='Monthly Revenue ...

  3. 谷歌浏览器提示Adobe Flash Player因过期而遭到阻止

    解决方法: 1.下载最新版本chrome://plugins/ 到官网Adobe Flash Player 下载最新版本,目前20 https://get.adobe.com/cn/flashplay ...

  4. LA3211 飞机调度 Now or later-二分法&TwoSet

    https://vjudge.net/problem/UVALive-3211 As you must have experienced, instead of landing immediately ...

  5. Android STL PORT

    ndk中包含了stl对应的库,在$(NKD_HOME)/sources/cxx-stl/stlport/stlport 有关Android NDK的C++ STL开发相关总结如下: 从Android ...

  6. powerdesigner逆向导出oracle数据库结构显示备注

    最近接到命令,要将oracle数据库的结构导出为pdm文件供其他同事使用,逆向工程导出数据库结构比较方便,但是发现导出的数据库结构没有注释,这是很郁闷的事情: 查过网上很多资料都是sqlserver的 ...

  7. 部署Ossim

    650) this.width=650;" title="29-1.jpg" alt="095310750.jpg" src="http:/ ...

  8. WWF3动态修改工作流<第九篇>

    一.动态添加或移除工作流活动 首先添加一个顺序的空白工作流. 然后添加一个Winform程序,界面如下: 代码如下: namespace WinForm { public partial class ...

  9. Linux:/bin/bash和/bin/sh的区别

    bash.dash(/bin/bash和/bin/sh) 原文:http://www.cnblogs.com/dkblog/archive/2011/04/02/2003822.html Linux中 ...

  10. RPC通信(Windows版、Android版)

    1.RPC通信模型 2.调用截图 服务器端 PC客户端: Android客户端: 3.remotetea jrpcgen.jar:生成Java源文件 oncrpc.jar:框架通信调用 portmap ...