write a simple python program to find the maximum of any number of numbers as defined by the user

In this article, we will write a simple Python program that allows the user to find the maximum of any number of numbers as defined by the user.

The first step in writing this program is to create a function that takes a variable number of arguments. In Python, this can be done using the *args syntax. The *args keyword is used to pass a variable number of arguments to a function. In our program, we will use this syntax to allow the user to define an arbitrary number of numbers.

# Function to find maximum of any number of numbers
def find_max(*args):
    # Initialize maximum to the first number
    maximum = args[0]
    # Iterate through the numbers
    for num in args:
        # If a number is greater than maximum
        if num > maximum:
            # Update maximum
            maximum = num
    return maximum

# Take input from the user
numbers = list(map(int, input("Enter multiple numbers separated by space: ").split()))

# Test the function
print("The maximum among the given numbers is:",find_max(*numbers))

This program uses the input() function to take input from the user, which is then stored in the numbers variable. The input is expected to be a string of numbers separated by spaces, which is then converted into a list of integers using the map() and split() functions. Then the function find_max() is called with *numbers as argument, which unpack the list into multiple numbers and passed as arguments to the function. The program will then print the maximum number among the user-provided numbers.

Leave a Comment