Return to the top after results - python

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!')

Related

Python: Print largest even number from 3 inputs without using list and max()

The program first asks for 3 integers then computes for the largest even from the set. It will print None if there are no even numbers. We are not allowed to use built-in functions (like max()) and not allowed to import math libraries.
What code can I use so it does not print None when I input x = 11, y = 11, z = 8? The correct output should be 8.
def getMaximum(x,y,z):
if x >= y and x >= z:
return x
elif y >= x and y >= z:
return y
else:
return z
def getLargestEven(x,y,z):
LargestVal = getMaximum(x,y,z)
if LargestVal%2 == 0:
return LargestVal
elif x%2 == 0 and (x > y or x > z):
return x
elif y%2 == 0 and (y > x or y > z):
return y
elif z%2 == 0 and (z > x or z > y):
return z
else:
return None
x = int(input("Enter x: "))
y = int(input("Enter y: "))
z = int(input("Enter z: "))
print("Largest Even:", getLargestEven(x,y,z))
You can also use a simple loop structure to test for the maximum even number among three
def getLargestEven(x,y,z):
max_num = -99999999999999
for num in [x,y,z]:
if num%2==0:
if num>max_num:
max_num = num
return None if max_num ==-99999999999999 else max_num

tranform a for into a while but keep the same properties? [duplicate]

This question already has answers here:
Converting for loops to while loops in python
(3 answers)
Closed 2 years ago.
def p(l):
x = True
y = len(l)
for z in range(y):
if (sum(l[z+1:]) == sum(l[:z])):
x = False
return z
if x:
return -1
So I would like to transform the for in my code into a while but keep all the same properties is there any way to do it without disturbing the code it self?
If you just want to convert a for loop to a while loop, these two loops are equivalent:
for x in range(y):
# do stuff
x = 0
while x < y:
# do stuff
x += 1
You'll need to instantiate the z variable to 0 and then you can use a while loop to break once all elements of l have been checked.
Though in your case, I guess that it'll throw an indexError when it tries to do l[z+1], so you'll need y - 1 as the termination condition.
z = 0
while z < y:
if (sum(l[z+1:]) == sum(l[:z])):
x = False
return z
z += 1
Your code doesn't have any indents, but it seems, it should be like this.
range(y) means from 0 to y-1, so we start from 0: z = 0, and increase z by 1 every iteration: z += 1. The last value is y-1, so it must stop when z = y, so while conditions is z < y
x = True
y = len(l)
# for z in range(y):
z = 0
while z < y:
if (sum(l[z+1:]) == sum(l[:z])):
x = False
return z
z += 1 # increase z value
if x:
return -1

why is a for loop not appending a list

In the below code, the loops don't seem to be working.
powers = []
x = 0
y = 0
z = 1
for x in powers:
powers.append([])
for y in powers[x]:
powers[x].append(z ** y)
z = z + 1
if z < 1001:
continue
else:
break
x = x + 1
y = y + 1
z = 1
print(powers)
However, when I run this in the terminal, it simply returns an empty list for powers. It doesn't show an error message.
Please help.
your code is badly formatted, so please consider formatting, anyway some of your errors are code-comments.
powers = []
x = 0
y = 0
z = 1
for x in powers: #here you're overriding former declaration of x, also powers is empty so for doesn't have a single run
powers.append([]) ##code is not indented so this istruction execute only once
for y in powers[x]: ##powers[x] is an empty list
powers[x].append(z ** y)
z = z + 1
if z < 1001:
continue
else:
break
z = 1 ## this is outside the cycle
x = x + 1
y = y + 1
print(powers)

While-loop with an or-condition

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.

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

Categories

Resources