




Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Prepara i tuoi esami
Studia grazie alle numerose risorse presenti su Docsity
Prepara i tuoi esami con i documenti condivisi da studenti come te su Docsity
Trova i documenti specifici per gli esami della tua università
Preparati con lezioni e prove svolte basate sui programmi universitari!
Rispondi a reali domande d’esame e scopri la tua preparazione
Riassumi i tuoi documenti, fagli domande, convertili in quiz e mappe concettuali
Studia con prove svolte, tesine e consigli utili
Togliti ogni dubbio leggendo le risposte alle domande fatte da altri studenti come te
Esplora i documenti più scaricati per gli argomenti di studio più popolari
Ottieni i punti per scaricare
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Esercizi pratici e soluzioni per il laboratorio python incentrati sulle funzioni. Una guida chiara e concisa per comprendere e applicare le funzioni in python attraverso esempi concreti. Include esercizi sulla manipolazione di stringhe, calcoli geometrici e applicazioni finanziarie, fornendo spiegazioni dettagliate per ogni soluzione. Ideale per studenti e appassionati che desiderano migliorare le proprie competenze di programmazione in python. Gli esercizi coprono il conteggio di vocali e parole in una stringa, il calcolo di volumi e superfici di solidi geometrici, la gestione di bilanci bancari con interessi e la determinazione di sussidi finanziari basati sul reddito familiare. Ogni esercizio è accompagnato da una spiegazione chiara del codice, rendendo il documento uno strumento didattico efficace e completo. Perfetto per chi cerca di approfondire la propria conoscenza delle funzioni in python attraverso la pratica e l'analisi di esempi reali.
Tipologia: Esercizi
1 / 8
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!





def count_vowels(string): VowelsNumber = 0
for ch in string: if ch.lower() in 'aeiou': VowelsNumber += 1
return VowelsNumber
def main(): StringEntered = input("Enter the string: ") print(f"Number of vowels in your string: {count_vowels(StringEntered)}")
main()
def count_words(string): counter = 0
for i in range(0, len(string), 1): if (string[i].isspace() and string[i-1].isalpha()) or i == len(string)-1: counter += 1
return counter
EExplanationxplanation The program counts how many vowels are in a string. The count_vowels function goes through each character in the string and, if it is a vowel, it increments a counter. In the main function, the user enters a string and the program prints the number of vowels found.
def main(): StringEntered = input("Enter the string: ") print(f"Your string has {count_words(StringEntered)} words.")
main()
Write the functions:
def sphere_volume(r) def sphere_surface(r) def cylinder_volume(r, h) def cylinder_surface(r, h) def cone_volume(r, h) def cone_surface(r, h)
that compute the volume and surface area of: a sphere with radius r, a circular-based cylinder with radius r and height h, a circular-based cone with radius r and height h.
Then write a program that asks the user for the values of r and h, calls the six functions, and displays the results in the output.
06.1.3 Geometric Solids
def sphere_volume(r): volume = (43.14(r**3))/ return volume
def sphere_surface(r): surface = 43.14(r**2) return surface
def cylinder_volume(r, h): volume = (3.14(r2))h return volume
def cylinder_surface(r, h): surface = 23.14(r2) + 23.14r*h return surface
def cone_volume(r, h): volume = (3.14(r2)h)/ return volume
def cone_surface(r,h): apothem = (r2 + h2)**(1/2)
surface = (3.14r)(apothem + r) return surface
The count_words function goes through each character and increments the counter whenever it finds a space after a letter or reaches the last character. In the main function, the user enters a string, and the program prints the number of words.
def assign_grant(Income, Children): Grant = 0 if Income > 30000 and Income <= 40000 and Children >= 3: Grant = Children* elif Income > 20000 and Income <= 30000 and Children >= 2: Grant = Children* elif Income <= 20000: Grant = Children*
return Grant
def main(): FamilyIncome = float(input("\nEnter the income: ")) NumberOfChildren = int(input("Enter the number of children in the family: ")) i = 1
while FamilyIncome != -1 and NumberOfChildren != -1:
print(f"Grant Family No. {i}: {assign_grant(FamilyIncome, NumberOfChildren)}") FamilyIncome = float(input("\nEnter the income: ")) NumberOfChildren = int(input("Enter the number of children in the family: ")) i += 1
main()
06.2.1 ONG
EExplanationxplanation
Write a program that converts a Roman numeral, like MCMLXXVIII, into its decimal representation. Hint : first, write a function that returns the numeric value of each single letter, then use the following algorithm:
total = 0 s = string representing the Roman numeral While s is not empty: If s has length 1, or the value of its first character is greater than or equal to the value of its
Add the value of the first character of s to total Remove the first character from s Else: difference = value of the second character of s - value of the first character of s Add difference to total Remove the first two characters from s
06.2.2 Roman numbers
second character
RomanLetters = 'IVXLCDM' CorrispondentValues = [1, 5, 10, 50, 100, 500, 1000]
def ValueRomLett(lett): Position = RomanLetters.find(lett) return CorrispondentValues[Position]
def ConvertToArabicNum(RomanNumber):
Total = 0 s = RomanNumber while len(s) != 0:
if len(s) == 1 or ValueRomLett(s[0]) >= ValueRomLett(s[1]): Total = Total + ValueRomLett(s[0]) s = s[1:]
else: Total = Total + ValueRomLett(s[1]) - ValueRomLett(s[0]) s = s[2:]
return Total
def main():
RomNum = input("Enter the Roman number: ") print(f"The value of the entered Roman number is: {ConvertToArabicNum(RomNum)}")
main()
EExplanationxplanation I define two lists, which are related by their indexes. Then I define a function that returns the value of a Roman letter. Next, I define the main function that converts a Roman numeral into an Arabic number. I keep converting until the Roman numeral is fully processed. If there is only one letter or the value of the first letter is greater than or equal to the second, I add its value and remove the first character. If the value increases, I calculate the difference and add it to the total, then remove the first two characters. Finally, I define main and run it.
06.2.4 Electric wire
def main(): awg = int(input("1) Enter an AWG value: "))
length_copper = float(input("\n2) Enter the length of the copper wire: ")) awg_copper = float(input("Enter the AWG value of the copper wire: "))
length_aluminum = float(input("\n3) Enter the length of the aluminum wire: ")) awg_aluminum = float(input("Enter the AWG value of the aluminum wire: "))
print(f'\n\n1) Diameter value: {diameter(awg)}') print(f'2) Copper Wire Resistance: {copper_wire_resistance(length_copper, awg_copper)}') print(f'3) Aluminum Wire Resistance: {aluminum_wire_resistance(length_aluminum, awg_aluminum)}')
main()
EExplanationxplanation The program computes the diameter of a wire from its AWG value and uses that diameter to calculate electrical resistance. One function returns the diameter based on the AWG formula, another computes copper resistance, and a third computes aluminum resistance. In the main function, the user enters lengths and gauges, and the program prints the corresponding diameter and resistance values.
Thank you Simone Altaserse