Finding the Largest Number among Three Inputs in Python

2023

In this project, we will discuss a Python code snippet that helps you find the largest number among three inputs. We'll explain each step of the code and provide the final working code at the end.


Getting User Input

To begin, we need to obtain three numbers from the user. We can use the input() function to prompt the user to enter the numbers. Let's store these inputs in variables called a, b, and c:

a = int(input("First number: "))
b = int(input("Second number: "))
c = int(input("Third number: "))

Finding the Largest Number

Next, we will define a function called find_largest() that takes three parameters: a, b, and c. Within the function, we will compare the values of a, b, and c to determine the largest number using conditional statements.

def find_largest(a, b, c):
    if (a >= b) and (a >= c):
        largest = a
    elif (b >= a) and (b >= c):
        largest = b
    else:
        largest = c
    return largest

The function compares a with b and c to see if a is the largest. If it is, a is assigned to the largest variable. Otherwise, it checks if b is larger than both a and c. If so, b becomes the largest value. If neither of these conditions is met, c must be the largest number.

Printing the Result

To display the largest number to the user, we can call the find_largest() function and pass the user inputs a, b, and c as arguments. We will use the print() function to show the result:

print("The largest number is: " + str(find_largest(a, b, c)))

In this line, we convert the returned value from find_largest() to a string using the str() function and concatenate it with the "The largest number is: " string.


Final Code

Here's the complete Python code that finds the largest number among three inputs:

def find_largest(a, b, c):
    if (a >= b) and (a >= c):
        largest = a
    elif (b >= a) and (b >= c):
        largest = b
    else:
        largest = c
    return largest

a = int(input("First number: "))
b = int(input("Second number: "))
c = int(input("Third number: "))

print("The largest number is: " + str(find_largest(a, b, c)))

That's it! You now have a Python code snippet that helps you find the largest number among three user inputs. Feel free to modify the code or use it as a starting point for your own projects. Happy coding!

Back