Šajā programmā jūs iemācīsities atrast lielāko starp trim skaitļiem, izmantojot ja citādi, un to parādīt.
Lai saprastu šo piemēru, jums jābūt zināšanām par šādām Python programmēšanas tēmām:
- Python ja … cits paziņojums
- Python ievade, izvade un imports
Tālāk programmā, trīs numuri tiek saglabāti num1
, num2
un num3
attiecīgi. Mēs izmantojām if… elif… else
kāpnes, lai atrastu lielāko no trim un parādītu tās.
Avota kods
# 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)
Rezultāts
Lielākais skaits ir 14,0
Piezīme: Lai pārbaudītu programmu, mainīt vērtības num1
, num2
un num3
.