Koodinäited¶
Tingimuslausetega koodijupp, mis prindib konsooli infot sisestatud numbri kohta.
number = float(input("Enter a number: "))
if number >= 0:
if number == 0:
print("The number is zero.")
else:
print("The number is positive.")
else:
print("The number is negative.")
Tingimuslausetega funktsioon (Funktsiooni mõiste ja kasutamine), mis esitab kuu sõnena.
def month(n):
"""
Returns the n-th month of the year as a string
:param n: month number
:return: month as string
"""
if n == 1:
return "January"
elif n == 2:
return "February"
elif n == 3:
return "March"
elif n == 4:
return "April"
elif n == 5:
return "May"
elif n == 6:
return "June"
elif n == 7:
return "July"
elif n == 8:
return "August"
elif n == 9:
return "September"
elif n == 10:
return "October"
elif n == 11:
return "November"
elif n == 12:
return "December"
else:
return "Incorrect month number."
print(month(8)) # --> August
print(month(12)) # --> December
print(month(0)) # --> Incorrect month number.