Here's a useful snippet that will show you how to use Python to find the largest number out of any three given numbers. Basically, the snippet works by using the if...elif...else statements to compare the three numbers against each other and determine which one is the largest. Check out the snippet below to see how it works:
number1 = 33 number2 = 67 number3 = 51 if (number1 > number2) and (number1 > number3): biggest = num1 elif (number2 > number1) and (number2 > number3): biggest = number2 else: biggest = number3 print("The biggest number between",number1,",",number2,"and",number3,"is",biggest)
In the example above, we know that num2 (67) is the biggest number. So the second statement (the elif one) is the correct one, because num2 is greater than both num1 and num3. For this reason, the results of executing the code would be the following output: "The biggest number between 33, 67 and 51 is 67." This works because 67 is set to be the "biggest" variable after the elif statement.
This code should work with any three numbers in determining which is the biggest. If you want your users to be able to input the numbers themselves, simply insert these three lines in your code in place of the other num1, num2, and num3 declarations:
num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) num3 = float(input("Enter third number: "))
Play around with the code to see if you can make it work with any combination of different numbers (you should be able to!).To Practice: Try this interactive course on the basics of Lists, Functions, Packages and NumPy in Python.