Home > Back-end >  Getting NameError: name 'count_even' is not defined , when trying to get the odd and even
Getting NameError: name 'count_even' is not defined , when trying to get the odd and even

Time:05-17

I am trying to get the even and odd numbers of a set but for some reason I am getting the error; NameError: name 'count_even' is not defined , what could the issue be?

count_odd=0
for select in [321,13,42,9785,20,33,834,903,22]:
    if select % 2 == 0:
       count_even=count_even   1
    else:
        count_odd=  count_odd   1
print(count_even)
print( count_odd)

CodePudding user response:

the error you get is obvious and self-explanatory. you didn't define count even in global scope that why you get the error.
below count_odd you need to define count_even
this is whole code

count_odd = 0
count_even = 0
for select in [321, 13, 42, 9785, 20, 33, 834, 903, 22]:
    if select % 2 == 0:
        count_even = count_even   1
    else:
        count_odd = count_odd   1
print(count_even)
print(count_odd)

CodePudding user response:

The solution is always on the error statement. You didn't specify count_even before using it in line 4

count_odd=0
count_even=0
for select in [321,13,42,9785,20,33,834,903,22]:
    if select % 2 == 0:
        count_even=count_even   1
    else:
        count_odd=  count_odd   1
print(count_even)
print( count_odd)
  • Related