鱼C论坛

 找回密码
 立即注册
查看: 762|回复: 1

35讲第0题:怎么改进一下循环这个 数字游戏

[复制链接]
发表于 2019-4-28 16:29:10 | 显示全部楼层 |阅读模式

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

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

x
我想可以不断的玩这个游戏,想到了用def建立函数,但是出了问题,请问应该怎么改,或者用另一种方法实现.
  1. import easygui as g
  2. import random


  3. def game():
  4.     search = random.randint(1, 10)
  5.     msg = '猜一下试哪个数字(1-10):'
  6.     title = '数字游戏'
  7.     shuru = g.integerbox(msg, title, lowerbound=1, upperbound=10)
  8.     while 1:
  9.         if shuru == search:
  10.             g.msgbox('你猜对啦')
  11.             if g.boolbox('你还想继续玩下去吗'):
  12.                 game()
  13.             else:
  14.                 break
  15.         else:
  16.             if shuru > search:
  17.                 g.msgbox('大了,大了')
  18.             else:
  19.                 g.msgbox('小了, 小了')
  20.             shuru = g.integerbox(msg, title, lowerbound=1, upperbound=10)
  21. game()
  22. g.msgbox('溜了溜了!')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-4-28 16:55:05 | 显示全部楼层
我搞出来了
  1. import easygui as g
  2. import random
  3. import sys

  4. def game():
  5.     search = random.randint(1, 10)
  6.     msg = '猜一下试哪个数字(1-10):'
  7.     title = '数字游戏'
  8.     shuru = g.integerbox(msg, title, lowerbound=1, upperbound=10)
  9.     while 1:
  10.         if shuru == search:
  11.             g.msgbox('你猜对啦')
  12.             if g.boolbox():
  13.                 game()
  14.             else:
  15.                 g.msgbox('溜了溜了!')
  16.                 sys.exit()
  17.         else:
  18.             if shuru > search:
  19.                 g.msgbox('大了,大了')
  20.             else:
  21.                 g.msgbox('小了, 小了')
  22.             shuru = g.integerbox(msg, title, lowerbound=1, upperbound=10)


  23. game()


复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-15 13:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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