Python program to sort negative numbers and even numbers?

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 4K views
acurate
Messages
17
Reaction score
1

Homework Statement


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.

The Attempt at a Solution



Code:
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?
 
Physics news on Phys.org
acurate said:

Homework Statement


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.

The Attempt at a Solution



Code:
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?
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.
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:
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.