鱼C论坛

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

[已解决]变量定义问题

[复制链接]
发表于 2016-2-1 19:01:27 | 显示全部楼层 |阅读模式

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

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

x
sun=int(input('输入一个整数'))
a=sun-1
while sun!=0:
    while a:
        print(" ",end='')
        a=a-1
    while sun:
        print("*",end='')
        sun=sun-1
    print()
    sun=sun-1

为什么
while sun:
        print("*",end='')
        sun=sun-1
这个语句会停不下来,这个是什么原理,sun=sun-1 这个最后不应该把sun变成0,然后while循环应该结束吗?
上面的语句我知道是错误的,我只是比较纠结于sun最后有没有变成0?
最佳答案
2016-2-1 19:22:55
本帖最后由 Lnan95 于 2016-2-1 19:28 编辑

因为sun=0时候在这里
  1.     while sun:
  2.         print("*",end='')
  3.         sun=sun-1
  4.     print()
  5.     sun=sun-1
复制代码

就是说sun=0,刚好出来while sun的循环,但是sun=0传不到while sun!=0,因为还要经历一个sun=sun-1,使sun=-1,所以就一直循环了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-2-1 19:22:55 | 显示全部楼层    本楼为最佳答案   
本帖最后由 Lnan95 于 2016-2-1 19:28 编辑

因为sun=0时候在这里
  1.     while sun:
  2.         print("*",end='')
  3.         sun=sun-1
  4.     print()
  5.     sun=sun-1
复制代码

就是说sun=0,刚好出来while sun的循环,但是sun=0传不到while sun!=0,因为还要经历一个sun=sun-1,使sun=-1,所以就一直循环了

评分

参与人数 2荣誉 +5 鱼币 +8 收起 理由
冷钟天 + 3 热爱鱼C^_^
冬雪雪冬 + 5 + 5 热爱鱼C^_^

查看全部评分

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

使用道具 举报

 楼主| 发表于 2016-2-2 07:32:49 | 显示全部楼层
Lnan95 发表于 2016-2-1 19:22
因为sun=0时候在这里

就是说sun=0,刚好出来while sun的循环,但是sun=0传不到while sun!=0, ...

囧,-1就等于TRUE。。。。。我居然忘记这个了,谢谢了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-19 12:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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