Simple python program ran into infinite loop - python

I am trying to make a simple XOR program. Once I finished checking my syntax, I ran my program and ran into an infinite loop. I can't find my error. Help?
def disencode(n):
seconde = raw_input("Input_Second_String")
y = len(n)
x = 0
while x < y:
if n[x] == seconde[x]:
print 0
else:
print 1
x =+1
disencode(raw_input("Input_First_String"))

x=+1 should be x += 1, as with your current code, you never increment x,
as x =+ 1 is the same thing as x = 1.
You're effectively setting x as 1, never increasing it, and asking the loop to run while x < y, which is infinite.
See here for more information

use x += 1 for incrementing x instead of x =+1

Related

Write a program using while loop, which prints the sum of every fifth number from 0 to 500 (including both 0 and 500)

This is what I have done:
Can anybody tell me where I went wrong?
num = 0
x = 1
while x <= 500:
num += x
x += 5
print(num)
Your indentation is also wrong. It needs to be like this
num = 0
x = 0
while x <= 500:
num += x
x += 5
print(num)
But, if you want to print only the final sum, you can use the print statement outside loop
num = 0
x = 0
while x <= 500:
num += x
x += 5
print(num)
Logic: We have to use while loop to check every number. We have to check whether the number is divisible by 5 or not, for that, we have to put if condition and keep on adding +1 to go from 0 to 500. If the condition is satisfied, add to to a variable whose initial value is 0. You code:
x=0
r=0
while x<=500:
if x%5==0:
r+=x
x+=1
print(r)
Or you can do it this way, start the initial value from 0, updating value of x should be 5, add it to variable whose initial value is 0. You alternative code:
x=0
r=0
while x<=500:
r+=x
x+=5
print(r)
The second code will help you rectify your personal code. Have a look
Yet another way:
x = 0
acc = 0
while (x := x + 5) <= 500:
acc += x
Here we use an assignment operator inside while() to get the value while incrementing it at the same time.

Program to find a specific prime number end in an infinite loop

Hey guys i have a problem i wrote this code for projecteuler to find a specific prime number but
it in ends in an infinite loop i looked it up and found many alternatives but want to understand why this code just dosent work. Im new to programing so if you also have any tipps to improve my code i will appreciate them.
import math
x = 1 #number that is getting checked
y = 0 #indicator of how many prime numbers found
a = 0 #the most recent prime number
while y < 6:
for i in range (2, int(math.sqrt(x))):
if (x % i ) == 0:
x = x + 1
break
else:
a = x
x = x + 1
y = y + 1
break
print (a)
You put x = 1, and then loop on a range starting from 2: so the for is never executed, and the while loops indefinitely. You need to start with x = 2, or to handle the special case of x = 1
EDIT
The code works for x at least 9: the for loop is never executed until int(math.sqrt(x)) is at least 3

for loop stuck and not interating (Python 3)

I'm currently working on a small script that aims to find taxicab numbers. There is only one small problem, the for loop doesn't increment the variable x and is stuck looping forever. I'll paste the relevant part of the code below:
n = int(10)
counter = int(0)
m = int(m)
x = int(1)
y = int(n**(1/3))
cheatsheet = []
while counter in range(0,m):
for x in range(1,y):
if x**3 + y**3 < n:
print('less than')
print(x,y,n)
continue
elif x**3 + y**3 > n:
print('greater than')
y -= 1
continue
elif x > y:
if n in cheatsheet:
print('counting')
counter = counter+1
#checking if n occurs in the list, if so it will add 1 to the counter so that the program will know when to stop
n = n + 1
y = int(n**(1/3))
x = 1
print('break1')
#resetting values and bumping n so that it will continue the quest to find 'em all
break
else:
if x and y == 1:
#this is an error correction for low n values, i mean really low it might be redundant by now
n = n + 1
y = int(n**(1/3))
x = 1
print('break2')
break
cheatsheet.append((n,x,y))
print(cheatsheet)
This yields the following result in a terminal window:
image
Note that I killed the process by myself, the program did not.
As you can tell, the script just loops around and prints 'less than' and the values for x,y,n.
Help is greatly appreciated!
EDIT: the variable m is given by the user, but not included in this bit of code.
This has a number of issues wrong with it I'm not willing to post functional code but I will point some of the issues out.
For starters:
n = int(10)
isn't required, not an error but redundant. Use n = 10 with the same effect.
Then:
while counter in range(0,m):
will always evaluate to True. m is *never modified so the membership test always succeeds, maybe you need to re-evaluate your looping.
for x in range(1,y):
This will assign x the value 1 all the time. y evaluates to 2 by your arithmetic (int rounds floats to the floor, i.e int(2.9) -> 2) so either use math.ceil or add one to your y.
Appart from that you re-assign variable names all the time inside your loops, that's confusing and might lead to unexpected behavior.

Project Euler 4 while loop issues

So I recently started trying to solve the Project Problems, and I'm trying to solve problem 4. I wrote code that should work, but a certain while loop refuses to run. Here is the code:
def project_euler_problem_4():
x = 998001
y = 999
while x > 10000:
if x == int(str(x)[::-1]):
while y > 100:
if x % y == 0:
print x
print y
print x/y
break
y = y -1
x = x -1
The problem arises when I tried to call the while loop after the if statement. My computer science teacher nor I have any idea what's causing problems. If you could help that would be great. Thanks!
In the innermost loop, y will become 99. It will never be reinitialized back to 999 again. So it will only ever run once.
Change it so that y is set back to 999 for the next test.
def project_euler_problem_4():
x = 998001
while x > 10000:
if x == int(str(x)[::-1]):
y = 999
while y > 100:
if x % y == 0:
print x
print y
print x/y
break
y = y -1
x = x -1

Why does this stop in Python 33

from math import *
def p():
for a in range (2, 100): ##for a in range 2 to 100
for y in range (2, a): ##for y in range 2, 100
x = 2*a+1 ##x has the following value
myprimelist = [] ##creating my empty list
if x > y:
while (x % y != 0):
myprimelist.append(x) ##append to list and continue with the same y until modulo is zero after which it reaches to else
y += 1
print (x,'nu se imparte egal la',y)
return [y] ##change the y
else:
y += 1
return [y]
else:
a += 1
return[a]
a += 1
return[a]
print (myprimelist);
I'm trying to make a function that will tell me all prime numbers from 2 to 100 but for some reason the loop stops and I don'know why. Please help. Thank you!
return means end of function. Everytime it is encountered the function stops executing.
It has nothing to do with python 3, its about return statement. You can try the following code as it checks only from 1 to sqrt(n), as it makes no point trying to check for numbers greater than sqrt(n). Hope this helps.
import math
for num in range(1,101):
if all(num%i!=0 for i in range(2,int(math.sqrt(num))+1)):
print num

Categories

Resources