在学习python中遇到一个问题,希望能得到大家的帮助,非常感谢
本帖最后由 tianalng233 于 2017-2-13 09:33 编辑首先是题目:
其次是代码:i = 1
test1 = (i%2 == 1)
test2 = (i%3 == 2)
test3 = (i%5 == 4)
test4 = (i%6 == 5)
test5 = (i%7 == 0)
test = test1 and test2 and test3 and tes4 and test5
while not test:
i += 1
print(i)
print(i)
最后是运行结果:运行结果是一个死循环.
如图:
运行结果是强行停止的.然后我找到的原因可能是由于 这一部分代码的问题.
test1 = (i%2 == 1)
test2 = (i%3 == 2)
test3 = (i%5 == 4)
test4 = (i%6 == 5)
test5 = (i%7 == 0)
test = test1 and test2 and test3 and tes4 and test5
while not test:
导致test永远为False,从而一直是死循环
我就想问一下,我的这串代码到底是为什么错的.
并且,我在python3的命令行中运行这个又没有问题,那就说明test = test1 and test2 这种形式是正确的. 无限循环的原因是,没有提供有效范围,导致所有符合要求的数都打印。 lubcat 发表于 2017-2-12 14:42
无限循环的原因是,没有提供有效范围,导致所有符合要求的数都打印。
你注意看一下运行结果.它输出的是所有的数,
而且,提供了有效范围之后.
代码如下:
i = 1
test1 = (i%2 == 1)
test2 = (i%3 == 2)
test3 = (i%5 == 4)
test4 = (i%6 == 5)
test5 = (i%7 == 0)
test = test1 and test2 and test3 and tes4 and test5
while i < 1000:
if test:
print(i)
else:
i += 1
print(i)#如果不加这句整个程序就会什么都不显示
以下是运行结果(ps:权限不够,以及 附件/图片 达到上传数目上限,无法上传):
2 3 4 5 6 7 8 9 10 11 .....996 997 998 999 1000
这就是运行结果
也就意味着test 永远为False
tianalng233 发表于 2017-2-12 15:36
你注意看一下运行结果.它输出的是所有的数,
而且,提供了有效范围之后.
代码如下:
test = test1 and test2 and test3 and tes4 and test5
这个判断方式不可复用。所以值一直为假,没有改变。 lubcat 发表于 2017-2-12 16:20
这个判断方式不可复用。所以值一直为假,没有改变。
那"不可复用"是为什么呢?可以在哪找到答案?
还有,我修改了以下代码.如果不可复用指的是这个样子的话:
i = 1
test1 = (i%2 == 1)
test2 = (i%3 == 2)
test3 = (i%5 == 4)
test4 = (i%6 == 5)
test5 = (i%7 == 0)
while i < 1000:
test = test1 and test2 and test3 and tes4 and test5
if test:
print(i)
else:
i += 1
print(i)#如果不加这句整个程序就会什么都不显示
那还是有问题,输出的结果仍然是从2到1000的自然数 lubcat 发表于 2017-2-12 16:20
这个判断方式不可复用。所以值一直为假,没有改变。
那"不可复用"是为什么呢?可以在哪找到答案?
还有,我修改了以下代码.如果不可复用指的是这个样子的话:
i = 1
test1 = (i%2 == 1)
test2 = (i%3 == 2)
test3 = (i%5 == 4)
test4 = (i%6 == 5)
test5 = (i%7 == 0)
while i < 1000:
test = test1 and test2 and test3 and tes4 and test5
if test:
print(i)
else:
i += 1
print(i)#如果不加这句整个程序就会什么都不显示
那还是有问题,输出的结果仍然是从2到1000的自然数 本帖最后由 lubcat 于 2017-2-12 16:46 编辑
tianalng233 发表于 2017-2-12 16:34
那"不可复用"是为什么呢?可以在哪找到答案?
还有,我修改了以下代码.如果不可复用指的是这个样子的话:
...
https://www.zhihu.com/question/51806214
知乎上的前辈的解答,共同学习下。 lubcat 发表于 2017-2-12 16:44
https://www.zhihu.com/question/51806214
知乎上的前辈的解答,共同学习下。
我主要纠结的就是
test = test1 and test2 and test3 and tes4 and test5
这个部分.
知乎上的这个部分
initValue = 7
while True:
if initValue % 2 == 1 and initValue % 3 == 2 and \
initValue % 4 == 3 and initValue % 5 == 4 and \
initValue % 6 == 5 and initValue % 7 == 0:
print(initValue)
break
else:
initValue += 7
作者:杨航锋
链接:https://www.zhihu.com/question/51806214/answer/127642855
来源:知乎
著作权归作者所有,转载请联系作者获得授权。
我知道这样做的是可以的.但是我就是想知道到底是哪出错了,
是test = test1 and test2 and test3 and tes4 and test5
这种方式根本就不行还是我的代码出问题.另外,非常感谢你的帮助! tianalng233 发表于 2017-2-12 17:04
我主要纠结的就是
这个部分.
问题就是这个test 判断语句,根本没有和循环体一起进行重复判断,所以test值只进行第一次判断,之后始终为假,也就只能一直输出所有数字。
这方式不可行。
具体你再比对下前辈的方法。 lubcat 发表于 2017-2-12 17:13
问题就是这个test 判断语句,根本没有和循环体一起进行重复判断,所以test值只进行第一次判断,之后始终 ...
根据你说的,我又修改了一下.结果仍然没有改变,还是2~1000.
i = 1
test1 = (i%2 == 1)
test2 = (i%3 == 2)
test3 = (i%5 == 4)
test4 = (i%6 == 5)
test5 = (i%7 == 0)
while i < 1000:
if test1 and test2 and test3 and test4 and test5:
print(i)
else:
i += 1
print(i)#如果不加这句整个程序就会什么都不显示
tianalng233 发表于 2017-2-12 17:18
根据你说的,我又修改了一下.结果仍然没有改变,还是2~1000.
我想我大概明白你的意思了.非常感谢.以下是修改后的代码.
i = 1
while i < 1000:
test1 = (i%2 == 1)
test2 = (i%3 == 2)
test3 = (i%5 == 4)
test4 = (i%6 == 5)
test5 = (i%7 == 0)
if test1 and test2 and test3 and test4 and test5:
print(i)
break
else:
i += 1
最后,非常非常感谢你的帮助 其实吧……
只要把每一步结果都算以下……
很容易发现问题……
在i ==1的时候……
1 % 2 == 1
1 % 3 == 1
1 % 4 == 1
……
所以test1 == 1(True),test2 == test3 == test4 == test5 == 0(False)
所以test == False(0)
于是下面循环就变成了:
i = 1
while True:
i += 1
print(i)
毫无疑问的陷入了死循环…… sd小舅子 发表于 2017-2-12 18:05
其实吧……
只要把每一步结果都算以下……
很容易发现问题……
是这样
test 作为布尔值,只经过了一次运算,之后的循环没有再参与 ,所以形成了死循环。
如果不把test1-test5的赋值放入循环中,test是不会有变动的。 tianalng233 发表于 2017-2-12 17:56
我想我大概明白你的意思了.非常感谢.以下是修改后的代码.
最后,非常非常感谢你的帮助
所有涉及条件判断的语句都需要加入到循环内,才能进行每次的条件判断。所以不能只把test放进去。
while的条件可以恒为真,<1000这个条件没起任何作用。
PS:
有个小疑问,10000以内满足要求的数,要如何修改可以 达成? lubcat 发表于 2017-2-12 18:28
所有涉及条件判断的语句都需要加入到循环内,才能进行每次的条件判断。所以不能只把test放进去。
while ...
i = 7
k = 0
while i <= 10000:
if (i%2 == 1) and (i%3 == 2) and (i%5 == 4) \
and (i%6 == 5):
print(i, end="")
#这部分是为了让结果每5个输出一行
k += 1
if k%5 == 0:
print("\n")
i += 7
这是代码 tianalng233 发表于 2017-2-12 18:47
这是代码
{:10_297:}
nice! 从第2行代码看不懂为什么那样子写 五行缺五行 发表于 2017-2-13 09:46
从第2行代码看不懂为什么那样子写
当时主要是想着这样可以让后面的条件判断简洁一点.所以想用一个变量名来代替,结果是在值为bool 类型的条件下,一个变量只能使用一次.所以就悲剧了 很久没用py了,但是记得py是严格遵守首行缩进,很多问题都是源于这样 加油加油{:5_105:}
页:
[1]