Python program to sort negative numbers and even numbers?

In summary: Yes, the code has a few errors that need to be fixed. First, the program should only add a number to the even_numbers list if it is divisible by 2 (using the modulus operator). Second, the if statement should check if the number is less than 0, not greater than or equal to 0. Finally, the list name for negative numbers should be "negative_numbers", not "negaitve_numbers". Overall, the code needs to be revised and debugged to properly separate and print the even and negative numbers.
  • #1
acurate
17
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?
 
Technology news on Phys.org
  • #2
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.
 
  • #3
@acurate, did you figure this out?
 

What is a Python program to sort negative numbers and even numbers?

A Python program to sort negative numbers and even numbers is a program written in the Python programming language that organizes a list of numbers into two separate lists - one for negative numbers and one for even numbers.

What is the purpose of sorting negative numbers and even numbers in a Python program?

The purpose of sorting negative numbers and even numbers in a Python program is to make it easier to analyze and manipulate these types of numbers separately from others in a list. This can be useful for various data analysis and mathematical operations.

What is the syntax for sorting negative numbers and even numbers in a Python program?

The syntax for sorting negative numbers and even numbers in a Python program may vary depending on the specific method or algorithm being used. However, in general, it involves using conditional statements and loops to identify and separate the negative and even numbers, and then sorting them using built-in functions or custom code.

Are there any limitations to sorting negative numbers and even numbers in a Python program?

Yes, there can be limitations depending on the specific implementation. For example, some methods may only work for a certain range of numbers or may not work for very large numbers. It is important to consider the limitations and choose a suitable method for the specific task at hand.

Can a Python program be used to sort other types of numbers besides negative and even numbers?

Yes, a Python program can be used to sort various types of numbers, including positive numbers, odd numbers, and fractions. The method and syntax may vary, but the overall concept of identifying and sorting specific types of numbers remains the same.

Similar threads

  • Programming and Computer Science
Replies
22
Views
776
  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
Replies
5
Views
890
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
4
Views
738
  • Programming and Computer Science
Replies
4
Views
394
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
781
Back
Top