0

It's a while loop and I need to add the log variable (most recent variable) to a list. I have no idea how to do it.

I'm going to add every number that is a multiple of both 5 and 7 (i.e. if x % 7==0 and x % 5==0) to a list which I am going to state at the end.

But how?

--

"This program is about the find every number between 1500 and 2700 (inclusive) which is divisible by both 5 and 7"

x=1500
for x in range(1500,2701):
    if x % 7==0 and x % 5==0:
        print("\n", x,"IS DIVISIBLE\n")
        x=x+1
        #I THINK THE LIST STUFF GOES HERE

    else:
        print(x,"is not a common multiple")
        x=x+1

input()

Basically, I just want the x variable which is divisible by 7 and 5 (each time the loop runs) to be added to a list. E.g. 1505, 1540 etc.

khelwood
  • 55,782
  • 14
  • 81
  • 108

5 Answers5

1

If you are looking for efficiency, do some math first:

  1. 5 and 7 are primes to themselves1 so a number is divisible from both if and only if it is divisible from their product.
  2. After finding the first number that satisfies point 1, simply keep adding 35 until you reach the end of your range. Note that you know how many such numbers will be in your range!

Now the code:

first = next(x for x in range(1500, 2701, 5) if x % 35 == 0)
res = [first + 35*i for i in range((2701-1-1500)//35 + 1)]

which produces:

[1505, 1540, 1575, 1610, 1645, 1680, 1715, 1750, 1785, 1820, 1855, 1890, 1925, 1960, 1995, 2030, 
 2065, 2100, 2135, 2170, 2205, 2240, 2275, 2310, 2345, 2380, 2415, 2450, 2485, 2520, 2555, 2590, 
 2625, 2660, 2695]

This will be faster than any if-based approach.


As far as the problem with your code goes, it has been thoroughly discussed by the other answers and comments, so I will not go into that.


1and in general but that is irrelevant here

Ma0
  • 15,057
  • 4
  • 35
  • 65
  • I am confused by the use of `next` here, as I never use it myself. How is that different from this code except for the fact that it's a one liner? `for x in range(1500, 2701, 5):` `if x%35 == 0:` `first = x` `break` – Guimoute Feb 12 '19 at 16:06
  • it is not; it is just a more compact way to write it. – Ma0 Feb 12 '19 at 16:13
  • Oh I get it. `next()` is simply the first element of the generator. Smart. – Guimoute Feb 12 '19 at 16:17
0

Create a list variable:

foo_list = []

After that, all you need to do is at the end of your loop append the value to your list

foo_list.append(<your variable>)

So it would look something like this on your code:

my_list = []
x=1500
for x in range(1500,2701):
    if x % 7==0 and x % 5==0:
        print("\n", x,"IS DIVISIBLE\n")
        my_list.append(x)
    else:
        print(x,"is not a common factor")

As you can see, the x+=1 was deleted, the loop is doing it for you!

As some people pointed out, if you are just interested in getting the list and not printing wether if the number is a common factor or not you could use list comprehensions, as follows:

my_list= [x for x in range(1500, 2701) if x%5==0 and x%7==0]
Marisa
  • 1,135
  • 3
  • 20
  • 33
0

You don't need to increment x when you have met a condition, as that's handled for you by the range generator.

You are close:

somelist = []

for x in range(1500, 2701):
    if x%5==0 and x%7==0:
        somelist.append(x)
    # You don't need an else block as the loop will just continue

You could alternatively do this in a list comprehension for more speed:

somelist = [x for x in range(1500, 2701) if x%5==0 and x%7==0]
C.Nivs
  • 12,353
  • 2
  • 19
  • 44
0
result =[]    
for x in range(1500,2701):
        if x % 7==0 and x % 5==0:
            result.append(x)    
        else:
            print(x,"is not a common factor")
            x=x+1
John Willson
  • 444
  • 1
  • 3
  • 13
0
my_list = []
for x in range(1500,2701):
    if x % 7==0 and x % 5==0:
        print("\n", x,"IS DIVISIBLE\n")
        my_list.append(x)
            #I THINK THE LIST STUFF GOES HERE


    else:
        print(x,"is not a common factor")
print(my_list)
kederrac
  • 16,819
  • 6
  • 32
  • 55