Darien Springer
Darien Springer

Reputation: 477

Input Function Within While Loop

In this program I am attempting to have it so that the variable integer_enter within the while loop will keep being assigned integers until a 0 is entered in to the input. Every time I run the code I get an EOF error on the "integer_enter= int(input())" line. Why would that be? There is nothing wrong with defining a variable within a while loop so why am I getting that error?

Code for reference:

list_num = [ ]
count_even = 0

loop_condition= True

while(loop_condition == True):
    integer_enter= int(input())
    integer_append= list_num.append(integer_enter)
    if(integer_enter % 2 == 0):
        count_even += 1
    elif(integer_enter == 0):
        loop_condition = False 




print('The number of even integers is %d' % count_even)

print(list_num)

Upvotes: 1

Views: 4007

Answers (1)

Divyanshu Maithani
Divyanshu Maithani

Reputation: 14986

You need to modify your condition for the loop termination. You need to make sure that your if is not executed when the integer_enter is 0. This is a working link to your code.

list_num = [ ]
count_even = 0

loop_condition= True

while(loop_condition == True):
    integer_enter= int(input())
    integer_append= list_num.append(integer_enter)
    if(integer_enter % 2 == 0 and integer_enter != 0):
        count_even += 1
    elif(integer_enter == 0):
        loop_condition = False 




print('The number of even integers is %d' % count_even)

print(list_num)

Upvotes: 1

Related Questions