guess number
crossin的前面几章基本和LPTHW内容重合,因此我直接做了他前面的一个综合练习。
猜数游戏,
即系统随机记录一个数,根据用户猜的记录,如果正确则告知,且退出游戏,如不正确,则提示答案与用户输入的比较。超过6次仍未猜对,则告知用户答案,且退出。
我在本章练习里,增加了一个列表,用以记录用户的输入记录,当用户失败时,告知他输入过哪些数字。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import random
def main():
random_num = random.randint(1,100)
user_input = []
for i in xrange(1,7):
user_num = int(raw_input("please input a num:\n>\t"))
if random_num == user_num:
print "BINGO!"
print "You guess the answer on %d time" % i
is_ok = True
break
elif random_num > user_num:
print "The answer is large then your input"
user_input.append(user_num)
is_ok = False
elif random_num < user_num:
print "The answer is less then your input"
user_input.append(user_num)
is_ok = False
if is_ok:
print "You win the game"
else:
print "You lose the game"
print "The answer is %d,your answer is %r" % (random_num,)
if __name__ == "__main__":
main()
考察点:
1、loop控制,其实while,for都可以很好的进行控制这个内容,在这里我没有选用while是因为while判断条件才进行循环的,如果条件控制不佳,容易造成死循环。而for循环的话,总能结束。
2、loop控制,关于答对题目时的退出,break,其实还有一种continue的控制方法,但是我没想到怎么加进去。continue的意思是,跳过本次循环,而break是跳出循环体。
3、布尔判断即if-elif-else
4、关于标准库的使用,即如果使用import导入必要模块等。
5、提高:其实可以使用try-except-finally进行用户输入,是否为数字的异常检测。我这里没写,如感兴趣可以给我留言。
6、变量赋值以及用户的输入。
guess number的更多相关文章
- 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 ...
随机推荐
- 移动端WEB开发备忘录
META相关 1. 添加到主屏后的标题(IOS) <meta name="apple-mobile-web-app-title" content="标题" ...
- SQL基础语句(提升)
1.复制表(只复制结构,源表名:a 新表名:b) select * into b from a where 1<>1 2.拷贝表 insert into b(a,b,c) select d ...
- Android Binder
http://blog.csdn.net/luoshengyang/article/details/6618363 Android进程间通信(IPC)机制Binder简要介绍和学习计划
- iOS开发技巧-2
1,打印View所有子视图 po [[self view]recursiveDescription] 2,layoutSubviews调用的调用时机 * 当视图第一次显示的时候会被调用 * 当这个视图 ...
- Unity3d之个性化皮肤
1.首先创建皮肤,贴图 2.在代码中定义public GUISkin变量,在Inspector中赋值 3.在OnGUI中调用 GUI.skin = mySkin; GUI.Button(new Rec ...
- Yii Uploadify批量上传
控制器: $reinfo = "fail"; $filename=""; //重要说明: //使用uploadify 上传时,每次这个sessionID都会改变 ...
- 阿伦学习html5 之Web SQL Database
不知道什么情况, W3C不再维护web SQL Database规范,但是大多浏览器都支持了! Web SQL Database规范页面有着这样的声明 Web SQL Database 规范中定义的三 ...
- reinstall ubuntu
flickering mouse issue http://askubuntu.com/questions/310341/do-graphics-drivers-for-intel-hd-4600-e ...
- VS2010里, using System.Data.OracleClient; 不可用
当我试图去引用System.Data.OracleClient 这个命名空间时,VS 显示不存在 但是在对象浏览器里却可以找到这个命名空间及里边的对象 另外好像也没有区分清楚 using 和Refer ...
- MySQL中DATETIME、DATE和TIMESTAMP类型的区别
一.TIMESTAMP 显示格式:YYYY-MM-DD HH:MM:SS 时间范围:[ '1970-01-01 00:00:00'到'2037-12-31 23:59:59'] TIMESTAMP D ...