Python: Display all even numbers between two integers inclusive, with a limit the number of times the code accepts input

Issue

This Content is from Stack Overflow. Question asked by brucey

Link to question: https://www.spoj.com/UTPEST22/problems/UTP_Q2/
From what I understand, the question is divided into 2 parts:

  1. Input
  • The first input is an integer that limits the number of time the user can provide a set of integers.
  • From the second line onwards, the user provides the sequence of integers up until the specified limit.
  • The set of integers are arranged in ascending order, separated only by a space each.
  1. Output
  • For each sequence of integers, the integers in it are looped over. Only those that are even are printed as outputs horizontally.
from sys import stdin

for x in range(1, 1+ int(input())):
# number of cases, number of times the user is allowed to provide input

    for line in stdin:
        num_list = line.split()
        # remove the space in between the integers
        num = [eval(i) for i in num_list]
        # change the list of numbers into integers each
        
        for numbers in range(num[0], num[-1] + 1):
        # the first integer is the lower bound
        # the second the upper bound
            if numbers % 2 == 0:
            # modulo operation to check whether are the integers fully divisible
                print(numbers, end = ' ')
                # print only the even numbers, horizantally

Can anyone please provide some insights on how to make changes to my code, specifically the loop. I felt so messed up with it. Screenshot of the result.

Any help will be appreciated.



Solution

Check the Answers

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?