Guess the Number

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

注释文档:

 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'


你可能感兴趣的:(Python)