-4

What I wanted to do in this program was, I tried to make some inputs and check them whether they are even or odd, and print them. But, as I run this, it is showing the following error message- (and see the image below)

for i in inp: TypeError: 'int' object is not iterable

Source code:

def number():
    n=int(input('How many numbers are there in your input: '))
    for inp in range(n):
        inp=eval(input('input= '))
    for i in inp:
        if i % 2 == 0:
            print(i, ' is even')
            continue
        else:
            print(i, ' is odd')
            continue
number()

enter image description here

Shafin Hasnat
  • 559
  • 7
  • 9
  • 2
    What's the purpose of using `eval` here? Also what do you think is the value of `inp` after the loop? – UnholySheep Jan 20 '18 at 12:30
  • 2
    Please post code and error messages here on SO, not pictures of it. – Mr. T Jan 20 '18 at 12:38
  • Two main points: 1 - Check how lists work -> https://www.makeuseof.com/tag/arrays-lists-in-python/ 2 - Please consider using Type Conversion Functions like `int()` instead of `eval()`-> https://en.wikibooks.org/wiki/Python_Programming/Data_Types#Type_conversion – manoelhc Jan 20 '18 at 13:19

2 Answers2

2

You need to use a list and append inputs to it. This way you can iterate it later.

 def number():
    n = int(input('How many numbers are there in your input: '))
    inp = []
    for i in range(n):
        p = input('input= ')
        if p.isdigit():
            inp.append(int(p))
    for i in inp:
        if i % 2 == 0:
            print(i, ' is even')
        else:
            print(i, ' is odd')

number()
mehrdadep
  • 972
  • 1
  • 15
  • 37
1
def number():
  n=int(input('How many numbers are there in your input: '))
  inp = []
  for i in range(n):
    inp.append(int(input('input= ')))
  for i in inp:
    if i % 2 == 0:
        print(i, ' is even')

    else:
        print(i, ' is odd')

number()
Pratik Kumar
  • 2,211
  • 1
  • 17
  • 41
  • 1
    use of `eval()` will cause errors if input is given something like `02` instead of `2` because it is used to evaluate an expression where as `int()` is used for type conversion. Also is the use of `continue` necessary in this case? [refered this](https://stackoverflow.com/questions/45881547/python-what-is-the-difference-between-eval-and-int) – Pratik Kumar Jan 20 '18 at 15:11