# Homework Help: Python program to sort negative numbers and even numbers?

Tags:
1. Mar 30, 2015

### acurate

1. The problem statement, all variables and given/known data
I have to make a program that would end when entered a 0 and print out negative numbers and even numbers separately but what I have so far is not working.

3. The attempt at a solution

Code (Text):

numbers = []
negative_numbers = []

while True:
number = input("Enter a number: ")
if number == "0":
print ("A zero has been entered.")
break
for i in number:
number = int(number)
if i >= 0:
numbers.append(i)
else:
negaitve_numbers.append(i)

print ("Numbers: ", numbers)
print ("Negative numbers: ", negative_numbers)

Any suggestions?

2. Mar 30, 2015

### Staff: Mentor

Here are several.
1. Instead of numbers for one of your lists, I recommend calling it even_numbers, to be more suggestive of what you'll put in it.
2. Instead of checking for number == "0" convert the input right away to an integer.
Code (Python):
entry = input("Enter a number")
number = int(entry)
After that, your logic looks like if number == 0:
3. Don't use a for loop. Just continue on with more elif clauses. This code makes no sense to me:
Code (Text):
for i in number:
4. To check whether a number is even, use the modulus operator %. The expression n % 2 evaluates to the remainder when n is divided by 2. If n is odd, the remainder is 1. If n is even (i.e., divisible by 2), the remainder is 0.
5. Fix the typo in your last line.

3. Apr 1, 2015

### Staff: Mentor

@acurate, did you figure this out?