-4

I am relatively new to programming and am having trouble with a simple while loop. I am attempting to write a program that outputs all multiples of 3 between 0 and 100. Here is what I have:

counter3 = 1
theList = []

while counter3 <= 1000:
   if 3 % counter3 : True
   theList.append(counter3)
   counter3 = counter3+1


print(theList)

The while loop I am using has a syntax error, but I am not sure where it is. Any help would be appreciated

deceze
  • 510,633
  • 85
  • 743
  • 889
JoeDoe
  • 3
  • 2
  • Please read https://stackoverflow.com/editing-help to learn how to properly format your question. – 001 Jan 19 '18 at 22:15
  • 3
    The syntax error should literally have a dashed line and a `^` that points to the exact place in your code that has the error. – rickdenhaan Jan 19 '18 at 22:15
  • 1
    Your code says: If counter3 mod 3 is trueish, then `True`. And that simply does nothing. – deceze Jan 19 '18 at 22:17
  • 2
    By the way, if I copy/paste your exact code into a `test.py` file and run it, Python 2.7 and 3.6 do not give a syntax error. The modulo doesn't work so they both just output all 999 numbers, but no errors. – rickdenhaan Jan 19 '18 at 22:21

2 Answers2

2

When the code you used is ran, it does not throw a syntax error. It instead returns a list of the numbers from 1 to 1000. The reason why is the following statement:

if 3 % counter3 : True
theList.append(counter3) 

The : operator denotes the end of the if statement, and what is placed after that is executed if the statement is true. In this scenario, you would want to replace the : with a ==. Since the if statement is executed automatically if 3 % counter3 is true, then you don't need == true at all.

Then, you would want to indent theList.append(counter3), so that it runs if the statement is true. New code:

counter3 = 1
theList = []

while counter3 <= 1000:
   if 3 % counter3:
       theList.append(counter3)
   counter3 = counter3+1


print(theList)

However, it still prints the numbers from 1 to 1000. This is because the numbers used in the % operator are on the wrong sides. You want to check if the number is divisible by 3, using the line if counter3 % 3:.

Now it prints everything except numbers divisible by 3. This is because a number divisible by 3 will return 0. 0 is a falsy value, which means it evaluates to false in the if statement. 1 and 2 (the other options) evaluate to true.

Falsy values are False, 0, [], "", 0.0, 0j, (), {}, and set().

Check this link for details.

Since you want to check if it is divisible by 3, then you want to check if the number mod 3 is equal to 0. Now your code is the following:

counter3 = 1
theList = []

while counter3 <= 1000:
    if counter3 % 3 == 0:
        theList.append(counter3)
    counter3 = counter3+1


print(theList)

You could do if not counter3 % 3, as 0 is falsy, but that obscures the function of your code and is not a good practice.

Since your question says to do numbers between 0 and 100, you should change the while statement to while counter3 <= 100:.

Another thing is that you should probably use a for loop. This lets you iterate over a range, allowing you to not have to keep track of the variable and increment it over time.

theList = []

for counter3 in range(1, 100):
    if counter3 % 3 == 0:
        theList.append(counter3)


print(theList)

This code works, and it observes most best practices.

If you wanted to, you could use a list comprehension for a one-liner, but this wouldn't be needed.

print([i for i in range(1, 100) if i % 3 == 0])

print([i * 3 for i in range(1, 34)]) #Prints first 33 numbers multiplied by 3, has the same result.
Milesman34
  • 177
  • 1
  • 6
0

To tell if a number is a multiple of three, it should be

if counter3 % 3 == 0:

counter3 % 3 computes the remainder of counter3 / 3, and this remainder is 0 for exact multiples.

Then you need to indent the statements that should be executed when this condition is true.

So the loop should be:

while counter3 <= 1000:
    if counter3 % 3 == 0:
        theList.append(counter3)
    counter3 = counter3+1

Instead of a while loop, you can use for:

for counter3 in range(1, 1001):
    if counter3 % 3 == 0:
        theList.append(counter3)
Barmar
  • 741,623
  • 53
  • 500
  • 612