Python Program to accept numbers from the user and Find the maximum and minimum element

Python Program to accept numbers and Find the maximum and minimum element of the numbers entered by the user

Problem Definition:

Write a Python Program to accept numbers from the user until the user enters done. Find the maximum and minimum element of the numbers entered by the user. Use class and methods concepts.

Program source code in Python to accept numbers and Find the maximum and minimum element of the numbers entered by the user

class Numbers:
    def __init__(self):
        self.values = []
        
    def maximum(self):
        max = 0
        for i in self.values:
            if i > max:
                max = i
        print ("Maximum element is ", max)
    
    def minimum(self):
        min = 999
        for i in self.values:
            if i < min:
                min = i
        print ("Minimum element is ", min)
        
    def insert(self):
        num = int (input("Enter the element: "))
        self.values.append(num)
        
x = Numbers()
while True:
    x.insert()
    str = input("Do you wish enter more elements press y otherwise enter done: ")
    if str == 'Done':
        break;
x.maximum()
x.minimum()

Output of Program:

Enter the element: 10
Do you wish enter more elements press y otherwise enter done: y
Enter the element: 20
Do you wish enter more elements press y otherwise enter done: y
Enter the element: 15
Do you wish enter more elements press y otherwise enter done: y
Enter the element: 48
Do you wish enter more elements press y otherwise enter done: y
Enter the element: 5
Do you wish enter more elements press y otherwise enter done: Done
Maximum element is  48
Minimum element is  5

Video Tutorial

This article discusses, how to write a Python program to accept numbers and Find the maximum and the minimum number of the numbers entered by the user. Subscribe to our YouTube channel for more videos and like the Facebook page for regular updates.

READ  Python program to count the frequency of each word in the file

Leave a Comment

Your email address will not be published. Required fields are marked *