鱼C论坛

 找回密码
 立即注册
查看: 1551|回复: 2

[已解决]新手关于while循环

[复制链接]
发表于 2017-5-2 17:06:33 | 显示全部楼层 |阅读模式

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

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

x
菜鸟刚开始学习,下面代码可以不断输入直至正确
import random
secret = random.randint(1,10)
guess = int(input("猜猜看:"))
while guess != secret:
    if guess > secret:
            print("大了")
    else:
            print("小了")

    guess = int(input("笨蛋,继续啊:"))
           
print("猜对了,聪明!")      
print("gameover")

而下面代码却不能
result = int(input("please input a number:"))
while result != 0 :
    if 60 <= result <= 100:
        print("ok")
    elif result < 60:
        print("bad")
    else:
        print("input wrong")
        
    reuslt = int(input("continue:"))
print ("over")
最佳答案
2017-5-2 17:16:39
本帖最后由 gopythoner 于 2017-5-2 17:17 编辑

看我的给你改的,2个地方,见注释
  1. result = int(input("please input a number:"))
  2. while result != 0 :
  3.     if 60 <= result <= 100:
  4.         print("ok")
  5.         break  #这里加一个跳出循环的语句
  6.     elif result < 60:
  7.         print("bad")
  8.     else:
  9.         print("input wrong")
  10.         
  11.     result = int(input("continue:"))  #你的result写错了,所以会出现result不刷新导致无限循环的问题
  12. print ("over")
复制代码
OZLU%~L(R]IVKV7ZY0@3LLG.png
`4~WACNYS)ZNR$3S}Z2@INT.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-5-2 17:16:39 | 显示全部楼层    本楼为最佳答案   
本帖最后由 gopythoner 于 2017-5-2 17:17 编辑

看我的给你改的,2个地方,见注释
  1. result = int(input("please input a number:"))
  2. while result != 0 :
  3.     if 60 <= result <= 100:
  4.         print("ok")
  5.         break  #这里加一个跳出循环的语句
  6.     elif result < 60:
  7.         print("bad")
  8.     else:
  9.         print("input wrong")
  10.         
  11.     result = int(input("continue:"))  #你的result写错了,所以会出现result不刷新导致无限循环的问题
  12. print ("over")
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-2 17:19:17 | 显示全部楼层
gopythoner 发表于 2017-5-2 17:16
看我的给你改的,2个地方,见注释

谢谢!我还得好好学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-17 13:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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