简体   繁体   English

嵌套的 While 循环未按预期工作

[英]Nested While loop not working as expected

I am very new to Python.我对 Python 很陌生。 I just learnt about for and while loops.我刚刚了解了 for 和 while 循环。 There was this exercise given to us to print all prime numbers from a list.有一个练习让我们打印列表中的所有素数。 I did the following我做了以下

my = [4,5,7,16,17,35,20,37]

for i in my:
    j = 2
    while j < i:
        if i % j == 0:
            break
        j = j + 1
    else:
        print(i)

And this worked but since i am just learning, out of curiosity I tried to achieve the result alternatively这行得通,但是由于我只是在学习,出于好奇,我尝试替代地实现结果

Instead of filtering and disallowing numbers as non prime in the if condition, I tried to do opposite ie evaluating numbers as prime in the if condition and use else to print non occurrence or prime number我没有在if条件中过滤和禁止数字作为非素数,而是尝试做相反的事情,即在if条件中将数字评估为素数并使用 else 打印非出现或素数

my = [4,5,7,16,17,35,20,37]

for i in my:
    j = 2
    while j < i:
        if i % j != 0:
            print(i)
        j = j + 1
    else:
        print("Prime No not found")

The issue with with this code it, its prints (i) as soon the first instance of j satisfies the if condition.这段代码的问题是,只要 j 的第一个实例满足 if 条件,它就会打印 (i)。 In the if statement, ideally I want it to first complete the while loop for j and print (i) only if all of the instance of j satisfies ( i % j is != 0 )在 if 语句中,理想情况下,我希望它首先完成 j 的 while 循环,并且仅当j的所有实例都满足时才打印 (i) ( i % j is != 0 )

With my current knowledge, I am unable to fix it.以我目前的知识,我无法修复它。 Can someone please rectify my code?有人可以纠正我的代码吗?

PS: I don't want a totally different or a advanced answer, just need changes in mine itself so i can understand at my beginner level. PS:我不想要一个完全不同或高级的答案,只需要我自己的改变,这样我就可以在我的初学者水平上理解。

In short, i am basically trying to use the if condition to store prime numbers and else for non prime, Unlike my first working code where i did the opposite简而言之,我基本上是在尝试使用 if 条件来存储素数,而 else 用于非素数,这与我的第一个工作代码相反,我做了相反的事情

It's not possible to reverse the logic from the first version of your code.不可能从您的代码的第一个版本中反转逻辑。 You need to run the whole inner loop before you can know the number you are testing is prime.您需要运行整个内部循环才能知道您正在测试的数字是素数。 You might find that it's not prime early (letting you quit and not test the rest of the potential divisors), but there's no instant check for primeness that you can do inside the inner loop.可能会发现它不是早期的素数(让您退出并且不测试潜在除数的 rest),但是您无法在内部循环中立即检查素数。 If there was, you wouldn't need the loop at all!如果有,你根本不需要循环!

Any effort to "fix" your second code would just turn it in to the first version you started with.任何“修复”你的第二个代码的努力都会把它变成你开始使用的第一个版本。 There are better prime-testing algorithms, but they're mostly a lot more complicated, and would fundamentally change the structure of your code.更好的素数测试算法,但它们大多要复杂得多,并且会从根本上改变代码的结构。

Observe the comments I implemented in the code:观察我在代码中实现的注释:

my = [4,5,7,16,17,35,20,37]

for i in my:
    j = 2
    while j < i:
        if i % j == 0: # As long as there's a single division that results in 0 remainder, the number is not prime
            break
        j = j + 1
    else:
        print(i)

Since you don't have a break statement inside the while loop below, the else statement will always be initiated.由于您在下面的while循环中没有break语句,因此将始终启动else语句。 That is the whole point of an while else statement.这就是while else语句的全部要点。 So you need to add a break somewhere.因此,您需要在某处添加break

my = [4,5,7,16,17,35,20,37]

for i in my:
    j = 2
    while j < i:
        if i % j != 0: # If a number is prime, all the cases will result in non-zero remainder, so many numbers will be print out
            print(i)
        j = j + 1
    else:
        print("Prime No not found")
my = [4,5,7,16,17,35,20,37]

for i in my:
    j = 2
    while j < i:
        if i % j != 0:
          print(i)
        break # you need to put the break out of if statement,so it will go to the next number to test, Hope this help
    else:
        print("Prime No not found")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM