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
Related
I'm currently experimenting on different ways to solve exponents(via addition). As soon as the results are printed, I could not use the program anymore.
I've tried continue and break, but they're not appropriate
x = int(input())
y = 0
z = x
while z != 0:
y += x
z -= 1
if z <= 0:
y *= x
print(y)
I just need to re-use the application again after the previous results, and keep using it until it is closed.
You will need to wrap everything in a while loop and give some type of exit condition.
x = input()
while x != 'exit':
x = int(x)
y = 0
z = x
while z != 0:
y += x
z -= 1
if z <= 0:
y *= x
print(y)
x = int(input())
print('You chose to end the program, goodbye!')
this is my code so far
x = 100000
while x < 100000:
y = x + 100000
z = (3*10000)-1/(10-3)
if y != z:
x += 1
else:
print(x)
break
I know the answer should be 42857 but it's giving me 10000
The loop never executes because of the wrong initialization of x. Besides, z is constant (doesn't depend on x)
In your code it probably prints 100000 because of the broken indentation that makes else statement match the while. Since no break occurs, the last value of x is printed.
Better do a for loop. That works:
for x in range (1,100000):
y = x + 100000
z = x*10 + 1
if y == z//3:
print(x)
break
else:
# for loop completed without break
print("not found")
or in one line, using next and a generator comprehension:
result = next(x for x in range (1,100000) if x + 100000 == (x*10 + 1)//3)
in both cases the result is indeed 42857
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
I want it to stop once one of the variables gets to the desired number. Why does this code wait until both variables equal 20 or greater to end?
z = 20
x = 1
y = 0
while x < z or y < z:
inp = int(input('enter a number'))
if x > y:
y += inp
elif y > x:
x += inp
print(x, y)
or using something like these examples just keeps adding and never stops:
while x != z or y != z:
while x or y < z:
while x or y != z:
If the loop must stop when at least one of the variables is >= z, then you must use and to connect the conditions:
while x < z and y < z:
In your code, by using or you state that as long as one of the variables is < z, the loop must continue - and that's not what you want.
I am trying to write the simplest code possible that will continuously print out Perfect Squares. My code so far reads this
x = 1
y = 1
while True:
final = x * y
print final
x = x + 1
y = y + 1
When I run it, I get a syntax error. The red bit is on the "final" in "print final"
Apologies if it is a really basic error, but I'm stumped.
Thanks for taking the time to read this
I assume you're using Python 3. print is now a function in Python 3.
Do print(final) instead of print final.
Also, it seems like your x and y values are holding the same thing. Why not discard one of them and use just one?
x = 1
while True:
final = x * x
print(final)
x = x + 1
Or better yet, use the builtin exponentiation operator **.
x = 1
while True:
final = x **2
print(final)
x += 1
Also, your code seems to be going into an infinite loop. You may need a condition to break it.
For example, if you want to break when x reaches 10, just add a condition in the while loop, like follows:
x = 1
while True:
final = x **2
print(final)
x += 1
if x == 10:
break
OR Specify a condition in the whilestatement, like follows:
x = 1
while x < 10:
final = x **2
print(final)
x += 1
OR Use a for loop.
for i in range(10):
print(i**2)
In Python 3, print is no longer a statement but a function, so you must enclose what you want to print in parentheses:
print(final)
Here's a link about the function.
You also have an IndentationError, y = y + 1 should be given a space.
And you can simplify that to y += 1 (which is the same thing, in regards to integers)
You can also add a condition to the while-loop:
x = 0
while x < 5:
print x ** 2
x += 1
Prints:
0
1
4
9
16
I would reccomend using Python 3 if you're not already. Also, as x and y are the same value at all times you only need one of them. So instead of reading:
x = 1
y = 1
while True:
final = x * y
print final
x = x + 1
y = y + 1
You should write:
x = 1
while True:
final = x * x
print(final)
x = x + 1
Hope this helped!
Jake.