猜数字小游戏

import random

import sys

num=random.randint(1,20)

print "------------------------------------------------------------------------------"

print '''Game rules:

There is a integer between 1 and 20.If you can guess it in six chances,you win the game,otherwise you lose the game.Good Luck!'''

print "------------------------------------------------------------------------------"

for i in xrange(1,7):

j=int(raw_input("Please guess the integer(1-20): "))

if j > num:

print "Too big!!You Still have %s chance!"%(6-i)

elif j < num:

print "Too small!!You Still have %s chance!"%(6-i)

elif j == num:

print "You win!!!"

sys.exit()

else:

print "You have already guess 6 times,You Lose!!!"

范例随机输出:

Game rules:

There is a integer between 1 and 20.If you can guess it in six chances,you win the game,otherwise you lose the game.Good Luck!

------------------------------------------------------------------------------

Please guess the integer(1-20): 2

Too small!!You Still have 5 chance!

Please guess the integer(1-20): 10

Too small!!You Still have 4 chance!

Please guess the integer(1-20): 15

Too small!!You Still have 3 chance!

Please guess the integer(1-20): 19

Too big!!You Still have 2 chance!

Please guess the integer(1-20): 18

You win!!!

原文发布于微信公众号 - 从零开始的linux(gh_4a9d788f5f27)

原文发表时间:2017-06-29

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券