Es. Python tutorial - Istruzioni condizionali

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

Es. Python tutorial - Istruzioni condizionali

Versione italiana

Esercizi pt. 1

  1. Esercizio 1: Parità Scrivi un programma che prenda un numero intero come input e stampi “Pari” se il numero è pari, altrimenti stampi “Dispari”.

  2. Esercizio 2: Maggiore di 10 Scrivi un programma che prenda un numero intero come input e stampi “Maggiore di 10” se il numero è maggiore di 10, “Minore di 10” se è minore di 10, e “Uguale a 10” se è uguale a 10.

  3. Esercizio 3: Voto Scrivi un programma che prenda un voto (da 0 a 100) come input e stampi “Insufficiente” se il voto è inferiore a 60, “Sufficiente” se è tra 60 e 74, “Buono” se è tra 75 e 89, e “Ottimo” se è 90 o superiore.

  4. Esercizio 4: Anno bisestile Scrivi un programma che prenda un anno come input e stampi “Anno bisestile” se l’anno è divisibile per 4 e non divisibile per 100, oppure se è divisibile per 400. Altrimenti, stampi “Anno non bisestile”.

  5. Esercizio 5: Saluto Scrivi un programma che prenda un’ora (in formato 24 ore) come input e stampi “Buongiorno” se l’ora è tra 6 e 11, “Buon pomeriggio” se è tra 12 e 17, e “Buona sera” se è tra 18 e 21. Se l’ora è al di fuori di questi intervalli, stampa “Buona notte”.

Risposte

  1. Esercizio 1:

    numero = int(input("Inserisci un numero: "))
    if numero % 2 == 0:
        print("Pari")
    else:
        print("Dispari")
  2. Esercizio 2:

    numero = int(input("Inserisci un numero: "))
    if numero > 10:
        print("Maggiore di 10")
    elif numero < 10:
        print("Minore di 10")
    else:
        print("Uguale a 10")
  3. Esercizio 3:

    voto = int(input("Inserisci un voto (0-100): "))
    if voto < 60:
        print("Insufficiente")
    elif 60 <= voto < 75:
        print("Sufficiente")
    elif 75 <= voto < 90:
        print("Buono")
    else:
        print("Ottimo")
  4. Esercizio 4:

    anno = int(input("Inserisci un anno: "))
    if (anno % 4 == 0 and anno % 100 != 0) or (anno % 400 == 0):
        print("Anno bisestile")
    else:
        print("Anno non bisestile")
  5. Esercizio 5:

    ora = int(input("Inserisci un'ora (0-23): "))
    if 6 <= ora <= 11:
        print("Buongiorno")
    elif 12 <= ora <= 17:
        print("Buon pomeriggio")
    elif 18 <= ora <= 21:
        print("Buona sera")
    else:
        print("Buona notte")

Esercizi pt. 2

  1. Esercizio 1: Età Scrivi un programma che prenda un’età come input e stampi “Minorenne” se l’età è inferiore a 18, “Maggiore” se è tra 18 e 64, e “Anziano” se è 65 o superiore.

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

  3. Esercizio 3: Giorno della settimana Scrivi un programma che prenda un numero da 1 a 7 come input e stampi il giorno della settimana corrispondente (1 per Lunedì, 2 per Martedì, ecc.). Se il numero non è compreso tra 1 e 7, stampa “Numero non valido”.

  4. Esercizio 4: Prezzo scontato Scrivi un programma che prenda un prezzo come input e stampi il prezzo scontato del 10% se il prezzo è superiore a 100, altrimenti stampa il prezzo originale.

  5. Esercizio 5: Classificazione di un triangolo Scrivi un programma che prenda tre lati di un triangolo come input e stampi “Triangolo equilatero” se tutti i lati sono uguali, “Triangolo isoscele” se due lati sono uguali, e “Triangolo scaleno” se tutti i lati sono diversi.

Risposte

  1. Esercizio 1:

    eta = int(input("Inserisci la tua età: "))
    if eta < 18:
        print("Minorenne")
    elif 18 <= eta < 65:
        print("Maggiore")
    else:
        print("Anziano")
  2. Esercizio 2:

    numero = float(input("Inserisci un numero: "))
    if numero > 0:
        print("Positivo")
    elif numero < 0:
        print("Negativo")
    else:
        print("Zero")
  3. Esercizio 3:

    giorno = int(input("Inserisci un numero da 1 a 7: "))
    if giorno == 1:
        print("Lunedì")
    elif giorno == 2:
        print("Martedì")
    elif giorno == 3:
        print("Mercoledì")
    elif giorno == 4:
        print("Giovedì")
    elif giorno == 5:
        print("Venerdì")
    elif giorno == 6:
        print("Sabato")
    elif giorno == 7:
        print("Domenica")
    else:
        print("Numero non valido")
  4. Esercizio 4:

    prezzo = float(input("Inserisci il prezzo: "))
    if prezzo > 100:
        sconto = prezzo * 0.10
        prezzo_scontato = prezzo - sconto
        print("Prezzo scontato:", prezzo_scontato)
    else:
        print("Prezzo originale:", prezzo)
  5. Esercizio 5:

    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:
        print("Triangolo equilatero")
    elif lato1 == lato2 or lato1 == lato3 or lato2 == lato3:
        print("Triangolo isoscele")
    else:
        print("Triangolo scaleno")

Es. Python tutorial - Conditional statement

English version

Exercises pt. 1

  1. Exercise 1: Parity Write a program that takes an integer as input and prints “Even” if the number is even, otherwise it prints “Odd”.

  2. Exercise 2: Greater than 10 Write a program that takes an integer as input and prints “Greater than 10” if the number is greater than 10, “Less than 10” if it is less than 10, and “Equal to 10” if it is equal to 10.

  3. Exercise 3: Grade Write a program that takes a grade (from 0 to 100) as input and prints “Fail” if the grade is less than 60, “Sufficient” if it is between 60 and 74, “Good” if it is between 75 and 89, and “Excellent” if it is 90 or higher.

  4. Exercise 4: Leap Year Write a program that takes a year as input and prints “Leap Year” if the year is divisible by 4 and not divisible by 100, or if it is divisible by 400. Otherwise, print “Non-Leap Year”.

  5. Exercise 5: Greeting Write a program that takes a time (in 24-hour format) as input and prints “Good morning” if the time is between 6 and 11, “Good afternoon” if it is between 12 and 17, and “Good evening” if it is between 18 and 21. If the time is outside these ranges, print “Good night”.

Answers

  1. Exercise 1:
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even")
else:
print("Odd")
  1. Exercise 2:
number = int(input("Enter a number: "))
if number > 10:
print("Greater than 10")
elif number < 10:
print("Less than 10")
else:
print("Equal to 10")
  1. Exercise 3:
grade = int(input("Enter a grade (0-100): "))
if grade < 60:
print("Failed")
elif 60 <= grade < 75:
print("Sufficient")
elif 75 <= grade < 90:
print("Good")
else:
print("Excellent")
  1. Exercise 4:
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap year")
else:
print("Non-leap year")
  1. Exercise 5:
time = int(input("Enter a time (0-23): "))
if 6 <= time <= 11:
print("Good morning")
elif 12 <= time <= 17:
print("Good afternoon")
elif 18 <= time <= 21:
print("Good evening")
else:
print("Good night")

Exercises pt. 2

  1. Exercise 1: Age Write a program that takes an age as input and prints “Minor” if the age is less than 18, “Major” if it is between 18 and 64, and “Senior” if it is 65 or higher.

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

  3. Exercise 3: Day of the Week Write a program that takes a number from 1 to 7 as input and prints the corresponding day of the week (1 for Monday, 2 for Tuesday, etc.). If the number is not between 1 and 7, it prints “Invalid Number”.

  4. Exercise 4: Discount Price Write a program that takes a price as input and prints the 10% discount price if the price is greater than 100, otherwise it prints the original price.

  5. Exercise 5: Classifying a Triangle Write a program that takes three sides of a triangle as input and prints “Equilateral Triangle” if all sides are equal, “Isosceles Triangle” if two sides are equal, and “Scalene Triangle” if all sides are different.

Answers

  1. Exercise 1:
age = int(input("Enter your age: "))
if age < 18:
print("Underage")
elif 18 <= age < 65:
print("Underage")
else:
print("Elderly")
  1. Exercise 2:
number = float(input("Enter a number: "))
if number > 0:
print("Positive")
elif number < 0:
print("Negative")
else:
print("Zero")
  1. Exercise 3:
day = int(input("Enter a number from 1 to 7: "))
if day == 1:
print("Monday")
elif day == 2:
print("Tuesday")
elif day == 3:
print("Wednesday")
elif day == 4:
print("Thursday")
elif day == 5:
print("Friday")
elif day == 6:
print("Saturday")
elif day == 7:
print("Sunday")
else:
print("Invalid number")
  1. Exercise 4:
price = float(input("Enter price: "))
if price > 100:
discount = price * 0.10
discount_price = price - discount
print("Discounted price:", discount_price)
else:
print("Original price:", price)
  1. Exercise 5:
time = int(input("Enter a time (0-23): "))
if 6 <= time <= 11:
print("Good morning")
elif 12 <= time <= 17:
print("Good afternoon")
elif 18 <= time <= 21:
print("Good evening")
else:
print("Good night")

Commenti