Es. Python tutorial - Istruzioni condizionali
Versione italiana
Esercizi pt. 1
Esercizio 1: Parità Scrivi un programma che prenda un numero intero come input e stampi “Pari” se il numero è pari, altrimenti stampi “Dispari”.
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.
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.
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”.
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
Esercizio 1:
= int(input("Inserisci un numero: ")) numero if numero % 2 == 0: print("Pari") else: print("Dispari")
Esercizio 2:
= int(input("Inserisci un numero: ")) numero if numero > 10: print("Maggiore di 10") elif numero < 10: print("Minore di 10") else: print("Uguale a 10")
Esercizio 3:
= int(input("Inserisci un voto (0-100): ")) voto if voto < 60: print("Insufficiente") elif 60 <= voto < 75: print("Sufficiente") elif 75 <= voto < 90: print("Buono") else: print("Ottimo")
Esercizio 4:
= int(input("Inserisci un anno: ")) anno if (anno % 4 == 0 and anno % 100 != 0) or (anno % 400 == 0): print("Anno bisestile") else: print("Anno non bisestile")
Esercizio 5:
= int(input("Inserisci un'ora (0-23): ")) ora 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
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.
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.
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”.
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.
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
Esercizio 1:
= int(input("Inserisci la tua età: ")) eta if eta < 18: print("Minorenne") elif 18 <= eta < 65: print("Maggiore") else: print("Anziano")
Esercizio 2:
= float(input("Inserisci un numero: ")) numero if numero > 0: print("Positivo") elif numero < 0: print("Negativo") else: print("Zero")
Esercizio 3:
= int(input("Inserisci un numero da 1 a 7: ")) giorno 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")
Esercizio 4:
= float(input("Inserisci il prezzo: ")) prezzo if prezzo > 100: = prezzo * 0.10 sconto = prezzo - sconto prezzo_scontato print("Prezzo scontato:", prezzo_scontato) else: print("Prezzo originale:", prezzo)
Esercizio 5:
= float(input("Inserisci il primo lato: ")) lato1 = float(input("Inserisci il secondo lato: ")) lato2 = float(input("Inserisci il terzo lato: ")) lato3 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
Exercise 1: Parity Write a program that takes an integer as input and prints “Even” if the number is even, otherwise it prints “Odd”.
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.
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.
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”.
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
- Exercise 1:
= int(input("Enter a number: "))
number if number % 2 == 0:
print("Even")
else:
print("Odd")
- Exercise 2:
= int(input("Enter a number: "))
number if number > 10:
print("Greater than 10")
elif number < 10:
print("Less than 10")
else:
print("Equal to 10")
- Exercise 3:
= int(input("Enter a grade (0-100): "))
grade if grade < 60:
print("Failed")
elif 60 <= grade < 75:
print("Sufficient")
elif 75 <= grade < 90:
print("Good")
else:
print("Excellent")
- Exercise 4:
= int(input("Enter a year: "))
year if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap year")
else:
print("Non-leap year")
- Exercise 5:
= int(input("Enter a time (0-23): "))
time 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
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.
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.
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”.
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.
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
- Exercise 1:
= int(input("Enter your age: "))
age if age < 18:
print("Underage")
elif 18 <= age < 65:
print("Underage")
else:
print("Elderly")
- Exercise 2:
= float(input("Enter a number: "))
number if number > 0:
print("Positive")
elif number < 0:
print("Negative")
else:
print("Zero")
- Exercise 3:
= int(input("Enter a number from 1 to 7: "))
day 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")
- Exercise 4:
= float(input("Enter price: "))
price if price > 100:
= price * 0.10
discount = price - discount
discount_price print("Discounted price:", discount_price)
else:
print("Original price:", price)
- Exercise 5:
= int(input("Enter a time (0-23): "))
time 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
Posta un commento