Lab05 (33817547)
Lab05 (33817547)
Exercise 1:
Write down a Python program to:
• read a positive integer number n.
• read n integer numbers from the keyboard.
• compute and display the average of the n values introduced by the user.
Example: let n = 5 and assume that the following 5 numbers are introduced: 2 4 6 8 10. Then, the
program must display the value 6.4 (as resulting from the computation: (2+4+6+8+10)/5).
Exercise 2:
Write down a Python program which:
• reads a positive integer number n.
• computes and displays the nth partial sum of the harmonic series: H(n) = 1 + 1/2 + … + 1/n
Example: assume that value 3 is introduced for n. Then, the program must compute the result of the
sum 1+1/2+1/3, showing a value of 1.83.
Exercise 3:
Write a Python program to use the loop to find the factorial of a given number. The factorial means
multiplying all numbers from the chosen number down to 1.
Exercise 4:
Write a Python program which can calculate the sum of the numbers which are divisible by 15,
between 100 and 300 (both included).
Exercise 5:
Example: the following (few) lines give the idea of what the program should display.
‘a’ 97 ‘A’ 65
‘b’ 98 ‘B’ 66
‘c’ 99 ‘C’ 67
‘d’ 100 ‘D’ 68
...
‘z’ 122 ‘Z’ 90
Exercise 7:
Write a Python program to construct the following pattern using a nested for loop.
Exercise 8:
Write down a Python program which:
• reads a positive integer number n.
• reads n integer values and:
o displays the message “ascending sequence” if every number of the sequence
(beyond the first one) is larger than the previous one.
o displays the message “descending sequence” if every number of the sequence
(beyond the first one) is smaller than the previous one.
o displays the message “neither ascending nor descending sequence”
if none of the above conditions is satisfied.
Exercise 9:
Write down a set of Python programs (one per figure) able to:
• read a positive integer number n.
• display the geometric figures (with “side” equal to n) as detailed in the following examples.
Example: assume n=4. Then, the figures to be printed are the following ones:
Example: assume n=5. Then, the figures to be printed are the following ones:
Exercise 10:
Write a Python program which repeatedly reads numbers until the user enters stop. Once stop is
entered, print out the total, count, and average of the numbers. If the user enters anything other than
a number, detect their mistake using try and except, and print an error message, and skip to the
next number.
Sample Output:
Enter a number: 15
Enter a number: 10
Enter a number: 20
Enter a number: bad input
You entered invalid input
Enter a number: 10
Enter a number: stop
Sum: 55 ** Number of inputs: 4 ** Average: 13.75