项目描述: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. 学习opencv跟轮廓相关的

    查找轮廓 轮廓到底是什么?一个轮廓一般对应一系列的点,也就是图像中的一条曲线.表示的方法可能根据不同情况而有所不同.有多重方法可以表示曲线.在openCV中一般用序列来存储轮廓信息.序列中的每一个元素 ...

  2. gem5 运行x86全系统仿真

    使用gem5可以启动Linux内核,称为全系统模拟,启动之后,可以通过telent连接,进行访问,但四telent有时不稳定,gem5推荐使用m5term进行连接访问,整个步骤如下: (1)打开终端, ...

  3. 洛谷P1518 两只塔姆沃斯牛 The Tamworth Two

    P1518 两只塔姆沃斯牛 The Tamworth Two 109通过 184提交 题目提供者该用户不存在 标签USACO 难度普及+/提高 提交  讨论  题解 最新讨论 求数据 题目背景 题目描 ...

  4. 开源Cheops软件在企业中的应用

    笔者在所属分公司的网络改造的网络改造中采用了开源软件作为其网络管理软件,曾经使用时间达2年多,没有出现过问题,其本身具备的主要管理功能完全可以商业的网管软件相媲美,下文将其部署心得和大家一起分享.一. ...

  5. css选择器分类

    css选择器大致可以分为10大类: 1.元素选择器 如:p{} 2.类选择器 如:.xx{} 3.ID选择器 如:#xx{} 4.关联选择器 如:p a{} 5.子元素选择器 如:p>a{} 6 ...

  6. curl返回常见错误码

    转自:http://blog.csdn.net/cwj649956781/article/details/8086337 CURLE_OK() 所有罚款.继续像往常一样. CURLE_UNSUPPOR ...

  7. yii 事物

    $transaction = Yii::app()->db->beginTransaction(); //创建事务 $transaction->commit(); //提交事务 $t ...

  8. ruby中symbol

    Symbol 是什么 Ruby 是一个强大的面向对象脚本语言(本文所用 Ruby 版本为1.8.6),在 Ruby 中 Symbol 表示“名字”,比如字符串的名字,标识符的名字. 创建一个 Symb ...

  9. Unieap3.5-前台js用SQL语句执行数据请求

    执行UPDATE var sql=" update T_SS_SETTLEMENT_RECORD "+ " set CINVOICE_INFO_FLAG='Y',&quo ...

  10. ASP.NET MVC SignalR(1):背景

    系列目录:ASP.NET MVC SignalR 关键词:HTTP.轮询.WebSocket.Server-Sent Events.长轮询.forever frame. 1. HTTP HTTP(Hy ...