Home > OS >  Detect even in multiple input in python
Detect even in multiple input in python

Time:10-15

#i make this code for detect, is user input have even or odd number.

a=integer(input('input multiple number : '))
if a %2==0:
print('have even number')
else:
print('have odd number')
#i want to put multiple numbers and separate them by spaces

#its just allow user for input 1 number, so I wanna make code for multiple input, so I just adding .split()

a=integer(input('input multiple number : ')).split()

#but it goes error because split() cannot work on integer, and I just make input without integer(), my operator doesn't work, please help :")

CodePudding user response:

Instead of putting integer at the input, try putting it when you are evaluating a.

for example:

if int(a)%2==0:
  print("even")

Also, int() is what you are looking for, not integer().

CodePudding user response:

Correct syntax to convert to a string input to an integer:

Input_variable = int(input())

CodePudding user response:

You can do it this way:

>>> numbers = input('Enter multiple numbers: ')
>>> numbers
'2 3 5 6'
>>> for number in numbers.split():
...             if int(number) % 2 == 0:
...                     print('even number')
...             else:
...                     print('odd number')
...
even number
odd number
odd number
even number
>>>
  • Related