In: Computer Science
Read in user-input integers one at a time until the user types ‘q’, storing all inputs in a list. Then, print out the even numbers in increasing order, from smallest value to largest. Python
The indentation in the answering window sometimes disappear automatically due to some technical issue on the website, please refer to the screenshots for indentation.
If you have any queries please comment in the comments section and kindly upvote.
# initialize an empty list
li=[]
# run an infinite loop until 'q' is entered
while(True):
# input element
ele=input()
# if the input is q then break
if ele=='q':
break
# otherwise append the element to the list after type casting it to
integer
else:
li.append(int(ele))
# sort the list
li.sort()
# iterate over the list
for i in range(len(li)):
# if the element in the list is even then print the element
if li[i]%2==0:
print(li[i],end=" ")