关闭

Guess the Number

77人阅读 评论(0) 收藏 举报
分类:

问题:点击打开链接 (练习二):

注释文档:

 1.程序询问是否想进行游戏:输入Yes

 2.程序产生随机数value

 3. 进入判断循环 , 输入数据guessNum 和 value产生对比,

 if(guessNum > value) print 'too high';

elif(guessNum < value) print 'too low' 

elif(guessNum == value) print 'correct'

发现相等时退出判断循环

# -*- coding: utf-8 -*-

import random

class Random(object):
    def __init__(self, _min, _max):
        self.min = _min
        self.max = _max
    def getValue(self):
        return random.randint(self.min, self.max) 

if __name__ == '__main__': 
    cnt = 0; flag = 0
    ins = Random(0, 100)   #取值范围为0-100
    while True:
        cnt = cnt+1
        while True:  
            if cnt == 1:              
                print 'Would you like to roll \\ (Yes or No)'  
            else :  
                print 'Would you like to roll again \\ (Yes or No)'  
            str = raw_input()  
            if str == 'Yes':  
                break  
            elif str == 'No':  
                flag = 1  
                break  
            else:  
                print 'Illegal input'  
        if flag == 1:
            break
        value = ins.getValue()
        while True:
             guessNum = int(raw_input())
             if guessNum == value:
                 print 'correct'
                 break
             elif guessNum > value:
                 print 'too high'
             elif guessNum < value:
                 print 'too low'


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:23159次
    • 积分:1193
    • 等级:
    • 排名:千里之外
    • 原创:99篇
    • 转载:12篇
    • 译文:0篇
    • 评论:0条