Python - Multiples of 7

Hi all,

so i have this question and i feel like i am getting it wrong, as i the result to me it seems ok…

What am i doing wrong?

question:
Write a script that prints the multiples of 7 between 0 and 100. Print one multiple per line and avoid printing any numbers that aren’t multiples of 7. Remember that 0 is also a multiple of 7.

my code:

for x in range(10):
  x<=100
  print (x*7)

it prints the multipies, but still i get this error:
Not quite. Review your code and make sure that it’s printing
the multiples of 7 between 0 and 100, that it prints them
one per line and that it doesn’t print any other numbers
that are not multiples of 7.

anyone knows why?
ty in advance

So you are looping over 0 till 10 with range, why? we need to go from zero to 100

then you have a comparison (x<=100), but the result of the comparison isn’t used for anything

furthermore, you only need to print numbers which are multiple of 7. So 0 (0 * 7), 49 (7 * 7), 98 (14 * 7) and so forth

so with my code, it prints this:

0
7
14
21
28
35
42
49
56
63

am i beeing dumb, or these are multiples of 7?!
ive put x<=100 because i thought will give me back only the numbers from 0 to 100 that are multiplies of 7
why range 0-10? cause if i put 100, it gives me lots of numbers i dont need. i am trying so hard to understand it, i feel its simple math, but at this point i simply dont get what i am doing wrong! xD

Hello @babygroot8236561007. Yes, those are multiples of 7, but they aren’t all of the multiples of 7 between 0 and 100 (what about 70, for example?) You don’t need the x <= 100:

Because all the x values are under 10, they will definitely be under 100. Also, if you wanted a conditional (if…else), this isn’t the correct syntax:

#correct syntax:
if objet == condition:
  #code 
#You can put any operator in place of the == (<= is one of them)
#Notice the 'if' and the ':'?

If you’re really stuck, I find the best way to work around it is to pause, and come back later. Then, when you come back, sit down, and see how you would do it if you were doing it in person. For example, sit down with a piece of paper, and think, Right. I need only the numbers between 0 and 100. I will write them all out. Then, I will see if they are divisible by 7. Think about the process involved in checking if a number (x) is divisible by another number (y). Once you come up with a method by which to do it, write instructions down as though you were writing them for your friend. This is now your pseudo-code. After that, see if you can translate that into Python syntax.
I hope this helps!

yeh, tried that already! but doesnt help…i may be dumb, but i just dont get it! maybe learning python without a teachter by doing exercises is not such a good ideea… :frowning:

Ok. This asks for you to check if a number is divisible by 7. Are you familiar with modulo (%)? modulo. Basically, it returns the remainder of a division:

return 4 % 2 #returns 0, because 4 / 2 is 2, no remainder
return 5 % 2 #returns 1, because 5 / 2 is 2, with 1 remainder.

Therefore, you could use something like:

if obj % 7 == 0:
#code

Basically, you could check to see if the number modulo 7 returns 0 (i.e. it is divisible by 7, because any number that divides and returns no remainder, is divisible).

If you're really stuck. I'm sure you won't need it!
def div_by_seven():#defines the function
for x in range(100):#loops through the numbers 0 to one hundred
if x % 7 == 0:#uses the modulo operator to check divisibility
#If the number divided by 7 returns no remainder, then execute the following code:
print(x)#print the number

This way, you get each number between 0 and 100 (exclusive, but 100 doesn’t divide into 7 anyway) that is divisible by 7.

I hope this helps!

thank you for your intention…but i still dont seem to get it right…

Hi. Could you repost your new code? Chances are there is just some slight error (indentation) that is causing it.

From looking at your previous attempt, I think you know roughly what you’re supposed to be doing but not quite how to implement it.

Since you’ve settled on a for loop for iteration, let’s stick with that.

Change the following steps into code. (If you’re ever stuck with a problem, doing this - and working out the process without involving code - will often help.)

  1. For every number between 1 and 100:
    1.1 Check if the number is a multiple of 7.
    1.2 If the number is a multiple of 7, print it to the console
    1.3 If the number is not a multiple of 7, do nothing.

This is not the most efficient way of doing it, but it’s essentially the algorithm for the approach you’re attempting. I’ve left some hints in my steps above as to how you can implement each of them…

Chances are they’ve copied the code from your previous post, which did not appear to be correctly indented. :slight_smile:

The code you posted...

for x in range(15):

print(x*7)

Welcome to the forums, but that two-liner doesn’t actually do what’s being asked at all. :slight_smile: