玩蛇网提供最新Python编程技术信息以及Python资源下载!
您现在的位置: 玩蛇网首页 > Python源码实例 > 正文内容

python初学者的猜数字游戏

python初学者的猜数字游戏,根据猜的次数打分
 
 

python初学者的猜数字游戏代码片段

1. [代码]猜数字游戏,有次数限制并且打分 

import random
num = random.randint(0,100)
times = 0

print "Number(0,100) guess game"
print "You will have 10 times to guess"
print "Guess out of range will game over immediately!"

guess = int(raw_input("guess a number:"))
times = times + 1
  
while 0 <= guess <= 100:
    if times == 10:
        if guess > num:
            print "Too high,The number is",num
            print "Guess times over,game over!"
        elif guess < num:
            print "Too Low,The number is",num
            print "Guess times over,game over!"
        else:
            print "You guess it.The number is:",num
            score = 10*(11-times)
            print "times is:",times
            print "Your score is",score
        break
    else:
        if guess > num:
            print "Too high,guess again!"
        elif guess < num:
            print "Too Low,guess again!"
        else:
            print "You guess it.The number is:",num
            score = 10*(11-times)
            print "times is:",times
            print "Your score is",score
            break
        guess = int(raw_input("guess a number:"))
        times = times + 1       
else:
    print "Your guess is out of range!Game over!"

玩蛇网文章,转载请注明出处和文章网址:http://www.iplaypy.com/code/c820.html [复制]



小额赞助,鼓励作者写更好的教程↓↓↓

玩蛇网Python互助QQ群,欢迎加入-->: 106381465 玩蛇网Python新手群
修订日期:2017年01月16日 - 16时30分36秒 发布自玩蛇网

我要分享到:

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules