鱼C论坛

 找回密码
 立即注册
查看: 3592|回复: 6

大佬们 这个为社么不能循环运行

[复制链接]
发表于 2022-9-28 20:46:20 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 渣渣苏 于 2022-9-28 20:50 编辑

counts = 3


while counts > 0 :
   temp = input("猜一猜我想的数字:")
   gusee = int(temp)

   if guess == 8:
      print("猜对啦")
   else:
       if guess < 8:
           print("小啦")
       else:
           print("大了")
       counts = counts - 1
print ("游戏结束")     



小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-9-28 20:54:24 | 显示全部楼层
本帖最后由 jackz007 于 2022-9-28 21:00 编辑
  1. counts = 3

  2. while counts > 0 :
  3.    temp = input("猜一猜我想的数字:")
  4.    guess = int(temp)         # guess 变量名写错

  5.    if guess == 8:
  6.       print("猜对啦")
  7.       break                  # 添加此句                  
  8.    else:
  9.        if guess < 8:
  10.            print("小啦")
  11.        else:
  12.            print("大了")
  13.        counts = counts - 1
  14. print ("游戏结束")
复制代码

       运行实况:
  1. D:\[00.Exerciese.2022]\Python>python x.py
  2. 猜一猜我想的数字:7
  3. 小啦
  4. 猜一猜我想的数字:4
  5. 小啦
  6. 猜一猜我想的数字:2
  7. 小啦
  8. 游戏结束

  9. D:\[00.Exerciese.2022]\Python>python x.py
  10. 猜一猜我想的数字:8
  11. 猜对啦
  12. 游戏结束

  13. D:\[00.Exerciese.2022]\Python>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-9-28 21:03:00 | 显示全部楼层
gusee = int(temp)  这里的gusee 改成 guess就可以了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2022-9-28 21:15:13 | 显示全部楼层

break的作用是什么
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-28 21:17:55 | 显示全部楼层
本帖最后由 jackz007 于 2022-9-28 21:20 编辑
渣渣苏 发表于 2022-9-28 21:15
break的作用是什么


       猜对就不需要继续循环了,所以,break 语句的作用就是,退出(或结束)循环
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-28 22:10:13 | 显示全部楼层
渣渣苏 发表于 2022-9-28 21:15
break的作用是什么

看看视频,都有
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-29 21:51:13 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-26 16:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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