1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Mar 30, 2015 #1
    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.")
        for i in number:
            number = int(number)
            if i >= 0:

    print ("Numbers: ", numbers)
    print ("Negative numbers: ", negative_numbers)
    Any suggestions?
  2. jcsd
  3. Mar 30, 2015 #2


    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.
  4. Apr 1, 2015 #3


    Staff: Mentor

    @acurate, did you figure this out?
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted