Write a Program That Reads a List of Integers Into a List as Long as The Integers Are Greater Than Zero Then Outputs The Smallest And Largest Integers in The List

Hi,  in this article you will learn a Python program that keeps reading the integer values and stores them in the list as long as user enters integers that are greater than zero. Your task is to find the smallest and largest integers present in the list.

Let's try to solve this using Python program.

 

 

Write a Program That Reads a List of Integers Into a List as Long as The Integers Are Greater Than Zero, Then Outputs The Smallest And Largest Integers in The List

 

 

Write a Program That Reads a List of Integers Into a List as Long as The Integers Are Greater Than Zero, Then Outputs The Smallest And Largest Integers in The List

Let's take an empty list of numbers(a new  list variable) and assign an empty list to it.  use the infinite while loop and ask a user to enter a number  using the input function call and then convert the input to Integer.

Use the if statement to check if the number is  greater than zero then find it to the list of numbers, Use the else block to break the  while loop.  if the user inputs a negative  number / zero then the while loop breaks.

numbers = []

while True:
   number = int(input("Enter a Number : "))
   if number > 0:
       numbers.append(number)
   else:
       break





Now let's take two variable smallest and largest and initialize it to   the first item containing in the list of integers,  I will be assuming  the first number present in the list is smallest and largest

Using the for loop, iterate all the numbers present in the list of integers and compare it with smallest and largest using the  if statements,  set the values accordingly based on the smallest and largest comparison as shown below.

smallest, largest = numbers[0], numbers[0]

for number in numbers:
   if number < smallest:
       smallest = number
   if number > largest:
       largest = number




Once the for loop completes the smallest and largest  variables will store the smallest and largest numbers present in the list.  using the print() function calls print the smallest and largest number present in the list.

print(f"Smallest Number Present in List is : {smallest}")
print(f"Largest Number Present in List is : {largest}")





Python Program
===========================


numbers = []

while True:
   number = int(input("Enter a Number : "))
   if number > 0:
       numbers.append(number)
   else:
       break

smallest, largest = numbers[0], numbers[0]

for number in numbers:
   if number < smallest:
       smallest = number
   if number > largest:
       largest = number

print(f"Smallest Number Present in List is : {smallest}")
print(f"Largest Number Present in List is : {largest}")






Output
=============
Enter a Number : 5
Enter a Number : 6
Enter a Number : 7
Enter a Number : 4
Enter a Number : 3
Enter a Number : 1
Enter a Number : 0
Smallest Number Present in List is : 1
Largest Number Present in List is : 7




Output
=========================
Enter a Number : 9
Enter a Number : 67
Enter a Number : 55
Enter a Number : 34
Enter a Number : 7
Enter a Number : 300
Enter a Number : 90
Enter a Number : 66
Enter a Number : -89
Smallest Number Present in List is : 7
Largest Number Present in List is : 300




Conclusion
================
This program is the basic implementation of getting the smallest and largest numbers present in the list of integers,  you can also use the built in functions to get the smallest and largest numbers present in the list.

Execute the above Python program by providing the random integer values as an input to it. Comment below if you have any suggestions to improve the above  Python program.

Post a Comment

0 Comments