Python program to sort negative numbers and even numbers?

Click For Summary
SUMMARY

The discussion focuses on creating a Python program that sorts negative and even numbers, terminating when a user inputs zero. The initial code contains several errors, including incorrect list naming and logic flaws in the loop structure. Key recommendations include renaming the list for even numbers, converting input to integers immediately, avoiding unnecessary loops, and using the modulus operator to determine even numbers. Additionally, a typo in the variable name for negative numbers must be corrected.

PREREQUISITES
  • Basic understanding of Python programming
  • Familiarity with data types and input handling in Python
  • Knowledge of control flow statements (if, elif, else)
  • Understanding of the modulus operator (%) for checking even numbers
NEXT STEPS
  • Implement a Python program using lists to separate negative and even numbers
  • Learn about Python input validation techniques
  • Explore Python's error handling to manage exceptions during input conversion
  • Study the use of functions to modularize the sorting logic in Python
USEFUL FOR

Students learning Python programming, educators teaching coding fundamentals, and developers seeking to improve their skills in input handling and data sorting in Python.

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?
 
Technology 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.
 

Similar threads

  • · Replies 28 ·
Replies
28
Views
5K
Replies
1
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 29 ·
Replies
29
Views
4K
Replies
5
Views
2K
Replies
12
Views
2K
Replies
17
Views
10K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K