IN PYTHON: Write a function listEvenNumbers2() using an infinite while loop. The
ID: 675050 • Letter: I
Question
IN PYTHON:
Write a function listEvenNumbers2() using an infinite while loop. The function takes NO input arguments. This will be a variation of problem 1, using a loop-and-a-half pattern. The function returns the list of even numbers entered by the user. An odd number ends the interactive input. Test using the following numbers when prompted: 100, 0, 4, 13
Recap: The loop-and-a-half pattern uses an infinite loop and one-way if statement inside the body of the while loop. The function will prompt for a number inside the while loop, and if statement will test whether the user entered the flag value.
Explanation / Answer
def listEvenNumbers2(): ans = [] while(1): num = input() num = int(num) if(num%2==0): ans.append(num) else: return ans #print(listEvenNumbers2())