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 ...
随机推荐
- PDF 补丁丁 0.5.0.2520 测试版发布:新春快乐!
新的PDF测试版今天发布了. 新的测试版比旧测试版本增加了如下功能: 合并文件功能可以导出.导入文件列表,方便合并大量文件.一天干不完,保存一下,明天继续来. 合并文件功能可以统一已合并文件的旋转方向 ...
- CSS3回执特殊图形
- JAVA类和对象课后作业
1.使用类的静态字段和构造函数,我们可以跟踪某个类所创建对象的个数.请写一个类,在任何时候都可以向它查询"你已经创建了多少个对象?" 代码: //显示类 //YiMingLai 2 ...
- UIImagePickerController和UIAlertController结合使用
在处理个人资料 - 头像的时候,通常有两个选项,一个是调用系统相机,一个是调用系统相册.这里要使用的就是UIImagePickerController方法. 在头像位置的imageView添加一个手势 ...
- 关于silverlight5 打印功能收集
http://www.cnblogs.com/slmk/archive/2012/07/18/2570303.html Silverlight打印解决方案2.1正式发布(支持打印预览.页面设置(横向纵 ...
- spring框架详解: IOC装配Bean
1 Spring框架Bean实例化的方式: 提供了三种方式实例化Bean. 构造方法实例化:(默认无参数) 静态工厂实例化: 实例工厂实例化: 无参数构造方法的实例化: <!-- 默认情况下使用 ...
- 开启关闭keditor 过滤
filterMode: false, K.create('#txt_content', { uploadJson: '/js/kindeditor-4.1.10/upload_json.ashx', ...
- hdu 1503, LCS variants, find a LCS, not just the length, backtrack to find LCS, no extra markup 分类: hdoj 2015-07-18 16:24 139人阅读 评论(0) 收藏
a typical variant of LCS algo. the key point here is, the dp[][] array contains enough message to de ...
- leetcode 日记 162. Find Peak Element java python
根据题目可知,输入为:一个相邻元素不相等的数列,输出为:其中一个(上)峰值的序号.并且要求时间复杂度为logn 分析:由于题目要求时间复杂度为logn,因此不能进行全部遍历.又因为只需要找到其中的一个 ...
- python获取字母在字母表对应位置的几种方法及性能对比较
python获取字母在字母表对应位置的几种方法及性能对比较 某些情况下要求我们查出字母在字母表中的顺序,A = 1,B = 2 , C = 3, 以此类推,比如这道题目 https://project ...