Im trying to teach myself a little bit of python and have become stuck/frustrated. Im on Python.org and so far have had no luck.

First, the objective is not too clear "Loop through and print out all even numbers from the numbers list in the same order they are received, but only up to the number "412" (not including it) Stop there and ignore all numbers past 412 in the sequence. (Note there will be numbers over 412.)"

Ive tried just filtering out the even numbers and i get back a list that is different from the expected outcome. I have double didget numbers where in the expected outcome is 3 didget numbers.

for theanswer in numbers:
    if theanswer % 2 == 0:
        print theanswer

Ive been working on this since last night and have changed the code about 40 times. Its probably something simple that i am missing, but i just cant figure this out.

My current code is

for theanswer in numbers:
    if theanswer ==412:
        continue
    if theanswer % 2 == 0:
        print theanswer

That filters out the 412 and prints only even numbers. But the expected outcome is still way off. Not in the same order and i still have double didget numbers also. I know i can add another if statement to remove the double didget numbers, but i feel like im going in the wrong direction here!?

Recommended Answers

All 3 Replies

Shortened the code up some

for num in numbers:
    if num != 412 and num % 2 == 0:
        print num

And if the num is 413, what your code does and what the request asks?

for printing all even numbers bellow 412 you can do this:

~~~for i in range(0,412):
if i%2==0:
print i~~~

and for a specific list of numbers:

~~~for num in num_list:
if (num<412) and (num%2==0):
print num~~~

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.