Python Script Hangs, Potentially an Infinite Loop?
Once again working on Project Euler, this time my script just hangs there. I'm pretty sure I'm letting it run for long enough, and my hand-trace (as my father calls it) yields no issues. Where am I going wrong?
I'm only including the relevant portion of the code, for once.
def main():
f, n = 0, 20
while f != 20:
f = 0
for x in range(1,21):
if n % x != 0: break
else: ++f
开发者_开发问答 if f == 20: print n
n += 20
Thanks in advance!
Python doesn't have increment (++
). It's interpreted as +(+(a))
. + is the unary plus operator, which basically does nothing. Use += 1
Here in your case 'f' value can never reach 20 and hence never exit 1) At 1st break (when n=20 and x =3) it again set f=0. Similarly for next loop also n get increased 20 but when 'x' is 3 again same f=0
So this will go in infinite loop....
精彩评论