前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >猜数字小游戏

猜数字小游戏

作者头像
零月
发布2018-04-25 16:06:32
7260
发布2018-04-25 16:06:32
举报
文章被收录于专栏:从零开始的linux

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!!!

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-06-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 从零开始的linux 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档