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的更多相关文章

  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. Mysql Innodb 引擎优化-内存、日志、IO、其他相关参数

    介绍: InnoDB给MySQL提供了具有提交,回滚和崩溃恢复能力的事务安全(ACID兼容)存储引擎.InnoDB锁定在行级并且也在SELECT语句提供一个Oracle风格一致的非锁定读.这些特色增加 ...

  2. web前端面试题

    HTML+CSS 1.对WEB标准以及W3C的理解与认识 标签闭合.标签小写.不乱嵌套.提高搜索机器人搜索几率.使用外链css和js脚本.结构行为表现的分离.文件下载与页面速度更快.内容能被更多的用户 ...

  3. LeetCode 3 Longest Substring Without Repeating Characters 区间,想法 难度:1

    https://leetcode.com/problems/longest-substring-without-repeating-characters/ 思路:从某点结束所能取到的最早开头是到目前出 ...

  4. MySQL5.5绿色版1067

    mysql的绿色安装版,按照很多文章进行配置,会出现 配置文件里面添加了 [client] default-character-set=utf8 [mysqld] default-character- ...

  5. C# winform TreeView中关于checkbox选择的完美类

    public static class TreeViewCheck { /// <summary> /// 系列节点 Checked 属性控制 /// </summary> / ...

  6. C# dll加载,抽象方法的使用

    抽象类! dll的使用 /// <summary> /// 返回类型--插件 /// </summary> /// <param name="baseName& ...

  7. Ant介绍

    今天介绍一下Ant,Ant是基于Java的跨平台构建工具,它易于使用,并且可扩展.可升级.它既可以用于小的个人项目,也可以用于大型的.多组协同的软件项目. 在我们的项目开发中,为了构建一个软件产品,我 ...

  8. $scope 的生命周期

    当Angular关心的事件发生在浏览器中时,比如用户在通过ng-model属性监控的输入字段中输入,或者带有ng-click属性的按钮被点击时,Angular的事件循环都会启动.这个事件将在Angul ...

  9. 008-Scala主构造器、私有构造器、构造器重载实战详解

    008-Scala主构造器.私有构造器.构造器重载实战详解 Scala主构造器实战 无参数的主构造器 分析 1.name 需要赋初值,一般通过占位符来代表空值 2.private 声明私有的age 生 ...

  10. 作业七:团队项目——Alpha版本冲刺阶段010

    今日安排:组内成员讨论 今日进程:组内成员讨论结束,并有明确的解决办法,每个人准备实践