Dato un certo orario è conforme o meno?
- 0 <= ore < 24
- 0 <= minuti < 60
- 0 <= secondi < 60
1
hh=...
mm=...
ss=...
RISPOSTA="Orario legale"
if(hh < 0):
RISPOSTA="Orario ILLEGALE."
if(hh >= 24):
RISPOSTA="Orario ILLEGALE."
if(mm < 0):
RISPOSTA="Orario ILLEGALE."
if(mm >= 24):
RISPOSTA="Orario ILLEGALE."
if(ss < 0):
RISPOSTA="Orario ILLEGALE."
if(ss >= 60):
RISPOSTA="Orario ILLEGALE."
print(RISPOSTA)
2
Più compatto
...
RISPOSTA = "Orario legale"
if(hh < 0) or (hh >= 24) or (mm < 0) or (mm >= 60) or (ss < 0) or (ss >= 60):
RISPOSTA = "Orario ILLEGALE"
print(RISPOSTA)
3
Senza la variabile RISPOSTA
if(hh < 0) or (hh >= 24) or (mm < 0) or (mm >= 60) or (ss < 0) or (ss >= 60):
print("Orario ILLEGALE.")
else:
print("Orario legale.")
4
Il controllo viene fatto sulla risposta complementare
if(hh >= 0) and (hh < 24) and (mm >= 0) and (mm < 60) and (ss >= 0) and (ss < 60):
print("Orario legale.")
else:
print("Orario ILLEGALE.")
5
Versione più compatta: sfrutta una caratteristica della sintassi di Python
if(0 <= hh < 24) and (0 <= mm < 60) and (0 <= ss < 60):
print("Orario legale.")
else:
print("Orario ILLEGALE.")
6
Per quale motivo l’orario è illegale?
if(hh < 0):
print("Orario ILLEGALE: ore negative.")
elif(hh >= 24):
print("Orario ILLEGALE: ore eccessive.")
elif(mm < 0):
print("Orario ILLEGALE: minuti negativi.")
elif(mm >= 24):
print("Orario ILLEGALE: minuti eccessivi.")
elif(ss < 0):
print("Orario ILLEGALE: secondi negativi.")
elif(ss >= 60):
print("Orario ILLEGALE: secondi eccessivi.")
else:
print("Orario legale.")
7
Più sintetico
if(hh < 0) or (hh >= 24):
print("Orario ILLEGALE: ore.")
elif(mm < 0) or (mm >= 60):
print("Orario ILLEGALE: minuti.")
elif(ss < 0) or (ss >= 60):
print("Orario ILLEGALE: secondi.")
else:
print("Orario legale.")