鱼C论坛

 找回密码
 立即注册
查看: 2258|回复: 3

[技术交流] 结合前几课内容做了一个游戏,求指导

[复制链接]
发表于 2015-12-19 18:19:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
大家好,我是刚开始学习python的小白。

结合前几节课的内容自己做了一个猜数字的游戏,跟课程中的有点区别的是:
提示的方式不是“大了小了“,而是数字的区间,而且区间会随着猜的内容越来越小。

代码如下:
  1. <font size="4">import random
  2. ans = random.randint(1,100)
  3. top = 100
  4. bottom = 1
  5. times = 5
  6. print('猜数字')

  7. temp = input('输入一个整数:')
  8. while temp.isdigit() == False or (int(temp) > 100 or int(temp) < 1):
  9.     while temp.isdigit() == False:
  10.         temp = input('数据类型错误,重新输入')
  11.     while int(temp) > 100 or int(temp) < 1:
  12.         temp = input('超出范围,请输入1~100的数字')
  13. num = int(temp)

  14. while num != ans and times > 1:
  15.     if num > ans:
  16.         top = num
  17.         print('错,答案介于'+str(bottom)+' ~ '+str(top)+'之间')
  18.     else:
  19.         bottom = num
  20.         print('错,答案介于'+str(bottom)+' ~ '+str(top)+'之间')
  21.     temp = input('还有'+str(times - 1)+'次机会,请继续猜:')
  22.     while temp.isdigit() == False or (int(temp) > 100 or int(temp) < 1):
  23.         while temp.isdigit() == False:
  24.             temp = input('数据类型错误,重新输入')
  25.         while int(temp) > 100 or int(temp) < 1:
  26.             temp = input('超出范围,请输入1~100的数字')
  27.     num = int(temp)
  28.     times = times - 1
  29. if times < 1:
  30.     print('Bingo! 游戏结束')
  31. else:
  32.     print('5次机会用尽还是没猜对,游戏结束')</font>
复制代码


但是我发现这个代码还是有点瑕疵,比如这段代码就重复了一次,整体不够简洁。
  1. <font size="4">while temp.isdigit() == False or (int(temp) > 100 or int(temp) < 1):
  2.         while temp.isdigit() == False:
  3.             temp = input('数据类型错误,重新输入')
  4.         while int(temp) > 100 or int(temp) < 1:
  5.             temp = input('超出范围,请输入1~100的数字')</font>
复制代码


大家看怎么改进比较好?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-12-19 19:20:06 | 显示全部楼层
我按照你的思路重写了一个,供参考。
  1. import random
  2. ans = random.randint(1,100)
  3. top = 100
  4. bottom = 1
  5. print('猜数字')
  6. for times in range(5, 0, -1):
  7.     if times != 5:
  8.         print('还有%d次机会,重新输入'%times)
  9.     while True:
  10.         try:
  11.             num = int(input('输入一个%d~%d的整数:'%(bottom, top)))
  12.         except ValueError:
  13.             print('数据类型错误,重新输入')
  14.         else:
  15.             if 1<= num <=100:
  16.                 break
  17.             else:
  18.                 print('超出范围,请输入1~100的数字')
  19.     if num == ans:
  20.         print('猜对了,答案是%d,Bingo! 游戏结束'%num)
  21.         break
  22.     elif num > ans:
  23.         top = num
  24.     else:
  25.         bottom = num
  26.     print('猜错了,答案介于%d~%d 之间'%(bottom, top))
  27. else:
  28.     print('5次机会用尽还是没猜对,游戏结束,正确答案是%d'%ans)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-12-20 17:18:33 | 显示全部楼层
冬雪雪冬 发表于 2015-12-19 19:20
我按照你的思路重写了一个,供参考。

3Q,学习了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

头像被屏蔽
发表于 2016-1-26 08:34:12 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 09:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表