hadsahar
hadsahar

Reputation: 23

Python: Is there a way to print even numbers within an unknown range and without if statement?

I have a homework to do in Python class and was given this question:

Make a program that gets 2 numbers from the user, and prints all even numbers in the range of those 2 numbers, you can only use as many for statements as you want, but can't use another loops or if statement.

I understand that I need to use this code:

for num in range (x,y+1,2):
    print (num)

but without any if statements, I can't check if the value x inserted is even or odd, and if the user inserted the number 5 as x, all the prints will be odd numbers.

I also tried to enter each number to a tuple or an array, but I still can't check if the first number is even to start printing.

def printEvenFor(x,y):
    evenNumbers =[]
    for i in range (x,y+1):
        evenNumbers.append(i)
    print (evenNumbers[::2])

or

def printEvenFor(x,y):
    for i in range (x,y+1,2):
        print(i,",")

I expect the output of printEvenFor(5,12) to be 6,8,10,12 but it is 5,7,9,11

Upvotes: 2

Views: 2776

Answers (8)

CAPSLOCK
CAPSLOCK

Reputation: 6483

def printEvenfor(x,y):
    return list(range(((x+1) // 2) * 2,y+1, 2))

printEvenfor(9,16)

Upvotes: 0

timgeb
timgeb

Reputation: 78650

Hacky but fun: multiplying strings with zero.

>>> low, high = int(input()), int(input())
5
13
>>> for n in range(low, high + 1):
...     print('{}\n'.format(n)*(not n%2), end='')
... 
6
8
10
12

Odd numbes are not printed because the string is multiplied with False (acting as zero).

Upvotes: 0

Rahul chandra
Rahul chandra

Reputation: 13

one way is by using while, that takes the start and end range in

for each in range(int(input()),int(input())):
    while each%2 == 0:
       print (each)
       break; 

Upvotes: 1

Alderven
Alderven

Reputation: 8260

You can use reminder to get the correct range:

def print_event_for(min_, max_):
    reminder = min_ % 2
    for i in range(min_+reminder, max_+reminder, 2):
        print(i)

print_event_for(5, 12)

Output:

6
8
10
12

Upvotes: 1

Max7cd
Max7cd

Reputation: 94

You can make x even, by using floor division and then multiplication:

x = (x // 2) * 2

x will then be rounded to the previous even integer or stay the same if it was even before.

If you want to round it to the following even integer you need to do:

x = ((x + 1) // 2) * 2

This can be improved further by using shifting operators:

x = (x >> 1) << 1         #Alternative 1
x = ((x + 1) >> 1) << 1   #Alternative 2

Examples:

#Alternative 1
x = 9
x = (x >> 1) << 1
#x is now 8

#Alternative 2
x = 9
x = ((x + 1) >> 1) << 1
#x is now 10

The second one is probably more suitable for you

Upvotes: 1

B.C
B.C

Reputation: 587

The following function will do what you want. I use round to force the boundaries to be even in order to get start the range on an even number.

def print_even_between(x, y):
    x = round(x / 2) * 2
    y = round(y / 2) * 2

    for i in range(x, y, 2):
        print(i)  
    print(y)

Upvotes: 0

BoarGules
BoarGules

Reputation: 16942

You can do it this way:

>>> for n in range((x + 1) // 2 * 2, y+1, 2):
        print(n)

The first argument to range forces it to be the next even number if it is odd. The last argument goes up in twos.

Upvotes: 0

Sebastian Dengler
Sebastian Dengler

Reputation: 1308

Try this:

x = x+x%2
for num in range (x,y+1,2):
    print (num)

Upvotes: 0

Related Questions