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)
Related
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
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!')
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.
To simplify my scenario, I am redefining my question here.
I want to add numbers from 1 to 5 in loop. X should be 1,2,3,4,5. and start with Y as 0. Y = X + Y should give the sum of 1 through 5.
Requirement: I want to start y as 0 and want y to hold latest add_sum value.
Expected output:
1st iteration: y = 1 (x = 1, y = 0)
2st iteration: y = 3 (x = 2, y = 1)
3st iteration: y = 6 (x = 3, y = 3)
...
...
so on
I am new to python coding, Can anyone help me for this?
Use reduce
reduce(lambda x, y: x + y, range(6))
As mentioned in the comments, fixing your syntax and running your code seems to work just fine.
def read_num():
for num in range (1,5):
x = num
add_sum(x)
def add_sum(x):
global y
y = x + y
print ("y =", y)
y = 0
read_num()
If you want x to be 1 thru 5 inclusive, you have to use range(1,6) instead
y = 0 # Assign value 0 to variable 'y'
for x in xrange(1, 6): # Cycle from 1 to 5 and assign it to variable x using iterator as we need just 1 value at a time
print '(x=%s, y=%s)' % (x, y) # Display value of 'x' & 'y' variables to user for debug & learning purpose
y += x # Adding x to the y.
print 'y=%s' % y # Display result of sum accumulated in variable 'y'
Edit: added comments to the code as requested in comments.
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.