5.9k views
0 votes
In Python please.

Create a program that reads integers from the user until a blank line is entered. Once all of the integers have been read your program should display all of the negative numbers, followed by all of the zeros, followed by all of the positive numbers. Within each group the numbers should be displayed in the same order that they were entered by the user.
For example, if the user enters the values 3, -4, 1, 0, -1, 0, and -2 then your program should output the values -4, -1, -2, 0, 0, 3, and 1. Your program should display each value on its own line.

1 Answer

4 votes

Answer:

negatives = []

zeros = []

positives = []

while True:

number = input("Enter a number: ")

if number == "":

break

else:

number = int(number)

if number < 0:

negatives.append(number)

elif number == 0:

zeros.append(number)

else:

positives.append(number)

for n in negatives:

print(n)

for z in zeros:

print(z)

for p in positives:

print(p)

Step-by-step explanation:

Initialize three lists to hold the numbers

Create a while loop that iterates until the user enters a blank line

Inside the loop:

If the number is smaller than 0, put it in the negatives list

If the number is 0, put it in the zeros list

Otherwise, put the number in the negatives list

When the while loop is done, create three for loops to print the numbers inside the lists

User PyjamaSam
by
8.3k points