first question on stackOverflow :) ! I would like to divide the range list by a different divider on each iteration of the for loop: For that I thought about having x
change on each iteration. I tried different things like making x
a list,and multiple other improvised ways, but to no avail. I have no idea why the code seems to iterate correctly but the value of x
doesn't change with x -= 1
.
The ultimate goal is to compare those lists and find similar evenly divisible numbers. But one thing at a time...
Here is the code:
def divisible(x):
lst1 = []
while x >= 2:
for each in range(0, 100001, 20):
if each % x == 0:
lst1.append(each)
x -= 1
return lst1
print(divisible(19))
It prints the first value of x
only:
[0, 380, 760, 1140, 1520, 1900, 2280, 2660, 3040, 3420, 3800, 4180, 4560, 4940...etc]
The issue is that your return statement is inside the while loop, which means that your function returns after the first iteration; preventing more iterations. Making this small modification results in this code:
def divisible(x):
lst1 = []
while x >= 2:
for each in range(0, 100001, 20):
if each % x == 0:
lst1.append(each)
x -= 1
return lst1
print(divisible(19))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.