-3

I wrote the following code, and it works well, but I would like to slightly modify it, so that from the range of numbers the last number is also tested for being an even number.

Is it possible to modify it so that the number being the stop_range is also tested for being even or uneven?

The code goes as follows:

counter = list(range(2))
numbers = []

for n in counter:
    numbers.append(int(input("Enter the number: ")))
numbers.sort()

print("Range of numbers: ", numbers)

even_num = []

for n in range(numbers[0], numbers[1]):
    if n == 0:
        pass
    elif n % 2 == 0:
        even_num.append(n)
    else:
        pass

print("Even numbers: ", even_num)

In the following example, I would like "10" also to be included in the even numbers list.

Enter the number: 5
Enter the number: 10
Range of numbers:  [5, 10]
Even numbers:  [6, 8]

Can this be done with the range function?

0

1 Answer 1

1

Simply change range(numbers[0], numbers[1]) to range(numbers[0], numbers[1]+1) for the checking range to include the last number

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.