129k views
0 votes
Code hints: while user_input >0: #This is the number that is greater than 0 from the list that is entered.

print(user_numbers) #Prints out the numbers greater than zero that are inputted at the top part from zyBooks.

Write a program that reads a list of integers into a list as long as the integers are greater than zero, then outputs the values of the list.

Ex: If the input is:

10
5
3
21
2
-6
the output is:

[10, 5, 3, 21, 2]
You can assume that the list of integers will have at least 2 values.

User Ajdin
by
6.9k points

1 Answer

6 votes

Final answer:

The program reads integers and stores them in a list using a while loop until a non-positive number is encountered. It then prints out the list of positive integers.

Step-by-step explanation:

To write a program that reads a list of integers and outputs only those that are greater than zero, you need to collect user input and append it to a list as long as the input is positive. Using a while loop will allow the program to keep asking for input until a non-positive number is entered. After that, the program should print the list of positive integers.

Here's a Python program that accomplishes that task:

user_numbers = [] # Initialize an empty list
while True: # Start an infinite loop
user_input = int(input()) # Get integer input from the user
if user_input <= 0: # Check if the input is less than or equal to zero
break # Exit the loop
user_numbers.append(user_input) # Append the positive integer to the list

print(user_numbers) # Output the list of positive integers

User AutomatedChaos
by
7.0k points