python小游戏1——猜数字

python也可以用来写个小游戏,自己玩玩,在这里练习了一个小游戏——猜数字。
在聚餐时,大家可能会玩猜数字游戏,规则是这样的:
指定一个猜数的范围,一个人在这个范围内出一个数,然后其他人依次地猜,猜的过程中区间不断缩小,直到有人猜中为止。

import random  
  
bot=input('Set range bottom\n')
top=input('Set range top\n')
rand=random.randint(bot,top)  
print ('Random number in ['+str(bot)+','+str(top)+'] generated!')  
num=int(input('###Guess the number###\n'))  
cnt=1  
while (num!=rand):  
    if (num<rand):  
        print('*_* Lower than the answer')  
    else:  
        print('T_T Higher than the answer')  
    num=int(input('###Guess the number###\n'))  
    cnt=cnt+1  
print('^_^ You get the answer with [%d] times'%cnt)

参考资料:
http://blog.csdn.net/buptlrw/article/details/41924849

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,638评论 25 709
  • ` 之前讲解了从scratch到python,基于python turtle库的实现,讲解了用scratch和py...
    少儿创客阅读 3,830评论 2 4
  • 2017年4月17日星期一晴 学经人员:琪佳妈 琪琪 佳佳。 宝贝年龄:琪琪8岁10个月,佳佳7岁7个月。 学经周...
    顺德琪佳妈阅读 185评论 0 1
  • 晚上在翻工具卡片时,翻到其中一张典型的行大于言的工具。且正好有件事让这个理论有了实践的机会。 家里从客厅到...
    cathy_2d4c阅读 452评论 0 0
  • 图、文//沐祺 你喜欢拿签字笔写硬笔书法, 我看到: 是你怕麻烦, 不喜欢做太多准备; 你喜欢找各种不同颜色和型号...
    沐祺阅读 729评论 0 1