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 ...
随机推荐
- ScrollView can host only one direct child
Android 采用ScrollView布局时出现异常:ScrollView can host only one direct child. 异常原因: 主要是ScrollView内部只能有一个子元素 ...
- input框只允许输入数字 --------20160705
//jquery方法 var num = $(this).val(); num = parseInt(num); if(!num){ $(this).html(''); } $(this).val(n ...
- HDU 4067 hdoj 4067 Random Maze 最小费用流
给出n个点,m条边,入口s和出口t,对于每条边有两个值a,b,如果保留这条边需要花费:否则,移除这条边需要花费b. 题目要求用最小费用构造一个有向图满足以下条件: 1.只有一个入口和出口 2.所有路都 ...
- 【浅层优化实战】ssh远程登录Linux卡慢的全过程排查及解决方案
ssh远程登录Linux卡慢的全过程排查及解决方案 前言: 在linux操作系统使用过程中偶然一次感到使用ssh远程连接软件连接操作系统需要等待许久,第一次没在意,第二次也没在意,第三次有点忍受不住了 ...
- xcode意外退出
完全不明所以的频繁退出 第一种 排除SVN冲突 在团队开发中,SVN冲突是最常见的了,程序异常时查看SVN文件冲突基本上成了本能. 排除SVN冲突 首先,右键主项目文件即xcodeproj文件,显示包 ...
- window下在同一台机器上安装多个版本jdk,修改环境变量不生效问题处理办法
window下在同一台机器上安装多个版本jdk,修改环境变量不生效问题处理办法 本机已经安装了jdk1.7,而比较早期的项目需要依赖jdk1.6,于是同时在本机安装了jdk1.6和jdk1.7. 安装 ...
- AngularJs的UI组件ui-Bootstrap分享(八)——Tooltip和Popover
tooltip和popover是轻量的.可扩展的.用于提示的指令.对于移动端来讲,这两个指令虽然可以正常工作,但是从用户体验的角度并不推荐使用. 先说tooltip,tooltip有三种使用方式: ( ...
- Octave安装和使用
1. Installer Download octave4.0 gnuplot 5.0.4 2. Installation (1) Octave 安装Octave时提示发现系统装有Win8,继续安装 ...
- WebService 生成类的命令语句
在开发项目中,有时候需要调用webservice接口程序,根据项目规定有的项目是直接引入接口,有的是需要把接口生成代理类的形式在项目中使用,根据项目需要来取舍. 以下列出项目中常用的Webservic ...
- Apple Pay的快速实现
一.在Apple开发者中心配置 AppleID 和 Merchant IDs 二.配置好证书后在Xcode中开启Apple Pay 三.代码实现 3.1 判断是否支持Apple Pay,如果支持又将支 ...