Odd and even number separator
Question:
I had been given this problem to write a code which will get a line of numbers on the input and will separate them into odd and even.
You get a line with natural numbers on the input. Sort them and write out the odd ones separately, the even ones separately
This is where had I gone so far, but I have mainly I think problem with separating and including the with the numbers from the input.
Input: 8 11 4 3 7 2 6 13 5 12
even: 6 4 2 6 12
odd: 11 3 7 13 5
a=[]
for i in range(1,100):
b=int(input().split())
a.append(b)
even=[]
odd=[]
for j in a:
if(j%2==0):
even.append(j)
else:
odd.append(j)
print("The even list:",even)
print("The odd list:",odd)
Answers:
try this
def separator(lst):
odd = []
even = []
for i in lst:
if i % 2 == 0:
even.append(i)
else:
odd.append(i)
return odd, even
Unclear why you need a loop to build any lists.
Just parse the input
a = map(int, input().split())
Then the rest of the code over a
to build two lists is fine
Firstly, why are you using a for loop? Secondly, you split the input into a list and pass it into the int function, which won’t work. You need to split the list, and convert each element into an int seperately. The rest is fine.
Working code:
lst = [int(num) for num in input("Enter space seperated numbers: ").split()]
even = []
odd = []
for num in lst:
if num % 2 == 0:
even.append(num)
else:
odd.append(num)
print(f"The even numbers are: {even}.")
print(f"The odd numbers are: {odd}.")
Your code to separate the odds and evens into separate lists looks okay, but your code to create the list from input in the first place looks wrong. You’re trying to convert a list generated by input().split()
into an int.
Instead use a list comprehension to make each element of that list into an int.
a = [int(x) for x in input().split()]
I had been given this problem to write a code which will get a line of numbers on the input and will separate them into odd and even.
You get a line with natural numbers on the input. Sort them and write out the odd ones separately, the even ones separately
This is where had I gone so far, but I have mainly I think problem with separating and including the with the numbers from the input.
Input: 8 11 4 3 7 2 6 13 5 12
even: 6 4 2 6 12
odd: 11 3 7 13 5
a=[]
for i in range(1,100):
b=int(input().split())
a.append(b)
even=[]
odd=[]
for j in a:
if(j%2==0):
even.append(j)
else:
odd.append(j)
print("The even list:",even)
print("The odd list:",odd)
try this
def separator(lst):
odd = []
even = []
for i in lst:
if i % 2 == 0:
even.append(i)
else:
odd.append(i)
return odd, even
Unclear why you need a loop to build any lists.
Just parse the input
a = map(int, input().split())
Then the rest of the code over a
to build two lists is fine
Firstly, why are you using a for loop? Secondly, you split the input into a list and pass it into the int function, which won’t work. You need to split the list, and convert each element into an int seperately. The rest is fine.
Working code:
lst = [int(num) for num in input("Enter space seperated numbers: ").split()]
even = []
odd = []
for num in lst:
if num % 2 == 0:
even.append(num)
else:
odd.append(num)
print(f"The even numbers are: {even}.")
print(f"The odd numbers are: {odd}.")
Your code to separate the odds and evens into separate lists looks okay, but your code to create the list from input in the first place looks wrong. You’re trying to convert a list generated by input().split()
into an int.
Instead use a list comprehension to make each element of that list into an int.
a = [int(x) for x in input().split()]