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、变量赋值以及用户的输入。

时间: 2024-08-07 17:01:33

guess number的相关文章

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

17. Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

实现一个函数clone,使JavaScript中的5种主要的数据类型(包括Number、String、Object、Array、Boolean)进行值复制

实现一个函数clone,可以对JavaScript中的5种主要的数据类型(包括Number.String.Object.Array.Boolean)进行值复制. 1 /** 对象克隆 2 * 支持基本数据类型及对象 3 * 递归方法 */ 4 function clone(obj) { 5 var o; 6 switch (typeof obj) { 7 case "undefined": 8 break; 9 case "string": o = obj + &q

解决sqoop报错Invalid number; item = ITEM_UNICODE

报错栈: java.sql.SQLException: Invalid number; item = ITEM_UNICODE at com.intersys.jdbc.SysList.getInt(SysList.java:1735) at com.intersys.jdbc.CacheResultSet.getInt(CacheResultSet.java:247) at org.apache.sqoop.lib.JdbcWritableBridge.readInteger(JdbcWrit

1005 Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

Minimum Inversion Number 【线段数】

Problem DescriptionThe inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of

171. Excel Sheet Column Number

Excel Sheet Column Number Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 static publi

hdu 5898 odd-even number 数位DP

odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 716    Accepted Submission(s): 385 Problem Description For a number,if the length of continuous odd digits is even and the length

Leetcode 202. Happy Number

202. Happy Number Total Accepted: 78171 Total Submissions: 208635 Difficulty: Easy Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace t

hdu 1394 Minimum Inversion Number(这道题改日我要用线段树再做一次哟~)

Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of