Program w Pythonie do znalezienia największej z trzech liczb

W tym programie nauczysz się znajdować największą z trzech liczb za pomocą polecenia if else i wyświetlać ją.

Aby zrozumieć ten przykład, powinieneś znać następujące tematy programowania w Pythonie:

  • Python if… else Instrukcja
  • Wejście, wyjście i import języka Python

W poniższym programie, trzy numery są przechowywane w num1, num2i num3odpowiednio. Użyliśmy if… elif… elsedrabiny, aby znaleźć największą z trzech i ją wyświetlić.

Kod źródłowy

 # Python program to find the largest number among the three input numbers # change the values of num1, num2 and num3 # for a different result num1 = 10 num2 = 14 num3 = 12 # uncomment following lines to take three numbers from user #num1 = float(input("Enter first number: ")) #num2 = float(input("Enter second number: ")) #num3 = float(input("Enter third number: ")) if (num1>= num2) and (num1>= num3): largest = num1 elif (num2>= num1) and (num2>= num3): largest = num2 else: largest = num3 print("The largest number is", largest) 

Wynik

 Największa liczba to 14,0 

Uwaga: Aby przetestować program, zmienić wartości num1, num2a num3.

Interesujące artykuły...