Es. Python tutorial - Istruzioni condizionali 2

Es. Python tutorial - Istruzioni condizionali 2 Es. Python tutorial - Istruzioni condizionali 2

Es. Python tutorial - Istruzioni condizionali 2

Versione italiana

Esercizi

  1. Esercizio 1: Numero intero positivo o negativo Scrivi un programma che prenda un numero intero come input e stampi “Numero positivo” se il numero è maggiore di zero, “Numero negativo” se è minore di zero, e “Zero” se è uguale a zero.

  2. Esercizio 2: Classificazione di un numero Scrivi un programma che prenda un numero intero come input e stampi “Numero pari” se il numero è pari e “Numero dispari” se è dispari.

  3. Esercizio 3: Fascia di età Scrivi un programma che prenda un’età come input e stampi “Bambino” se l’età è inferiore a 13, “Adolescente” se è tra 13 e 19, “Adulto” se è tra 20 e 64, e “Anziano” se è 65 o superiore.

  4. Esercizio 4: Verifica di un triangolo Scrivi un programma che prenda tre lati come input e verifichi se possono formare un triangolo. Stampa “Può formare un triangolo” se la somma di due lati è sempre maggiore del terzo, altrimenti stampa “Non può formare un triangolo”.

  5. Esercizio 5: Stagione dell’anno Scrivi un programma che prenda un mese (da 1 a 12) come input e stampi la stagione corrispondente: “Inverno” per dicembre, gennaio e febbraio; “Primavera” per marzo, aprile e maggio; “Estate” per giugno, luglio e agosto; “Autunno” per settembre, ottobre e novembre.

Risposte

Esercizio 1:

numero = int(input("Inserisci un numero intero: "))
if numero > 0:
    print("Numero positivo")
elif numero < 0:
    print("Numero negativo")
else:
    print("Zero")

Esercizio 2:

numero = int(input("Inserisci un numero intero: "))
if numero % 2 == 0:
    print("Numero pari")
else:
    print("Numero dispari")

Esercizio 3:

eta = int(input("Inserisci la tua età: "))
if eta < 13:
    print("Bambino")
elif 13 <= eta <= 19:
    print("Adolescente")
elif 20 <= eta <= 64:
    print("Adulto")
else:
    print("Anziano")

Esercizio 4:

lato1 = float(input("Inserisci il primo lato: "))
lato2 = float(input("Inserisci il secondo lato: "))
lato3 = float(input("Inserisci il terzo lato: "))
if (lato1 + lato2 > lato3) and (lato1 + lato3 > lato2) and (lato2 + lato3 > lato1):
    print("Può formare un triangolo")
else:
    print("Non può formare un triangolo")

Esercizio 5:

mese = int(input("Inserisci un mese (1-12): "))
if mese == 12 or mese == 1 or mese == 2:
    print("Inverno")
elif mese >= 3 and mese <= 5:
    print("Primavera")
elif mese >= 6 and mese <= 8:
    print("Estate")
elif mese >= 9 and mese <= 11:
    print("Autunno")
else:
    print("Mese non valido")

English version

Exercises

  1. Exercise 1: Positive or Negative Integer Write a program that takes an integer as input and prints “Positive Number” if the number is greater than zero, “Negative Number” if it is less than zero, and “Zero” if it is equal to zero.

  2. Exercise 2: Classifying a Number Write a program that takes an integer as input and prints “Even Number” if the number is even and “Odd Number” if it is odd.

  3. Exercise 3: Age Range Write a program that takes an age as input and prints “Child” if the age is less than 13, “Teen” if it is between 13 and 19, “Adult” if it is between 20 and 64, and “Senior” if it is 65 or above.

  4. Exercise 4: Checking a Triangle Write a program that takes three sides as input and checks whether they can form a triangle. Print “Can form a triangle” if the sum of two sides is always greater than the third, otherwise print “Cannot form a triangle”.

  5. Exercise 5: Season of the Year Write a program that takes a month (from 1 to 12) as input and prints the corresponding season: “Winter” for December, January and February; “Spring” for March, April and May; “Summer” for June, July and August; “Autumn” for September, October and November.

Answers

Exercise 1:

number = int(input("Enter an integer: "))
if number > 0:
print("Positive number")
elif number < 0:
print("Negative number")
else:
print("Zero")

Exercise 2:

number = int(input("Enter an integer: "))
if number % 2 == 0:
print("Even number")
else:
print("Odd number")

Exercise 3:

age = int(input("Enter your age: "))
if age < 13:
print("Child")
elif 13 <= age <= 19:
print("Teen")
elif 20 <= age <= 64:
print("Adult")
else:
print("Elderly")

Exercise 4:

side1 = float(input("Enter the first side: "))
side2 = float(input("Enter the second side: "))
side3 = float(input("Enter the third side: "))
if (side1 + side2 > side3) and (side1 + side3 > side2) and (side2 + side3 > side1):
print("Can form a triangle")
else:
print("Cannot form a triangle")

Exercise 5:

month = int(input("Enter a month (1-12): "))
if month == 12 or month == 1 or month == 2:
print("Winter")
elif month >= 3 and month <= 5:
print("Spring")
elif month >= 6 and month <= 8:
print("Summer")
elif month >= 9 and month <= 11:
print("Fall")
else:
print("Invalid month")

Commenti