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 ...
随机推荐
- 测试一下Word发布刚刚发现通过Word 可以直接发Blog 感觉很新奇,先看看,如果可以呢,将发通知的改了。
刚刚发现通过Word 可以直接发Blog 感觉很新奇,先看看,如果可以呢,将发通知的改了. 刚刚发现通过Word 可以直接发Blog 感觉很新奇,先看看,如果可以呢,将发通知的改了. ...
- java 将数据写进文件
/*每次只写入一行数据 只需要调用特定的方法即可.*/package com.second.File;import java.io.*;/** * Created by hasee on 2016/1 ...
- qml中打开本地html
main.cpp QString tmploc = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); QDi ...
- hdoj 5124lines
题意:给你n条线段,求被最多的线段覆盖的点被覆盖的次数 解法:我们可以将一条线段[xi,yi]分为两个端点xi和(yi)+1,在xi时该点会新加入一条线段,同样的,在(yi)+1时该点会减少一条线段, ...
- 转: Rest简介及Spring实现
一 Roy Fielding 2000年Rest被Roy Fielding提出来的,我对Roy Fielding的印象有以下几个. 一是RoyFielding做为Http协议的起草者,在Http协议发 ...
- 【 2013 Multi-University Training Contest 7 】
HDU 4666 Hyperspace 曼哈顿距离:|x1-x2|+|y1-y2|. 最远曼哈顿距离,枚举x1与x2的关系以及y1与y2的关系,取最大值就是答案. #include<cstdio ...
- Bootstrap <基础二十三>页面标题(Page Header)
页面标题(Page Header)是个不错的功能,它会在网页标题四周添加适当的间距.当一个网页中有多个标题且每个标题之间需要添加一定的间距时,页面标题这个功能就显得特别有用.如需使用页面标题(Page ...
- java 排序
class Employee { private String name; private String id; private String salary; public static void m ...
- c——I/O Multiplexing笔记
1. select第一个参数为最大FD(int)+1,因为虽然select参数里有三个set,但分配到的fd值是不会重复的,当select检查fd可用时(可读或可写或异常),会遍历进程fd表,这时遍历 ...
- 在Django中进行注册用户的邮件确认
之前利用Flask写博客时(http://hbnnlove.sinaapp.com),我对注册模块的逻辑设计很简单,就是用户填写注册表单,然后提交,数据库会更新User表中的数据,字段主要有用户名,哈 ...