美文网首页
python小游戏1——猜数字

python小游戏1——猜数字

作者: AlexDM | 来源:发表于2017-02-05 18:19 被阅读276次

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

相关文章

网友评论

      本文标题:python小游戏1——猜数字

      本文链接:https://www.haomeiwen.com/subject/eiveittx.html