Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

UNIVERSITY OF THE WESTERN CAPE

DEPARTMENT OF COMPUTER SCIENCE

COS101 2023
Term 2 – Practical 3 Friday

Practical 1 – Creating Simple Python Programs [20 marks]

This practical focuses on writing Python programs that use lists, Boolean expressions, logical and
numerical operators. Furthermore, the practical aims to give you more practice on functions,
loops, and conditional statements.

Please note that:


• You are required to do exercises 1, 2 and 3.
• Exercise 4: Optional

Exercise 1 – Create a Python program that uses functions [10 marks]


Write a program that performs simple calculations of addition, subtraction, and multiplication of
two integers. While the user has not typed in the word “Quit”, it should keep on asking the user
for:
• task: Addition, subtraction or multiplication
• the first number and the second number.
• Once the user is done, it should print the answer for the specific operation using the two
numbers.
The program should have an output similar to the example below. (Hint: Use a while loop and if
statements and it should have three functions).

Output:
Welcome to our Simple Calculator Program
Enter A for Addition
Enter S for Subtraction
Enter M for Multiplication
Enter Q to Quit. A

Please Enter the first Number 10

Please Enter the Second Number 20


Answer = 30

Welcome to our Simple Calculator Program


Enter A for Addition
Enter S for Subtraction
Enter M for Multiplication
Enter Q to Quit. q
Thanks for using our program, Bye!!
Exercise 2 - Create a Python program that uses a function [10 marks]

Part 1
Geometric Progression (GP) Program: Write a python program that uses a function to output a
GP given the initial term (never zero), common ratio and the number of terms in the sequence.
A GP is a sequence of elements in which the next number/term in the sequence is obtained by
multiplying the previous number/term by the common ratio. For example:
• 1,2,4,8,16: The initial term is 1, the common ratio is 2 and there are 5 terms in the
sequence.
• 2,6,18,54: The initial term is 2, the common ratio is 3 and there are 4 terms in the sequence.
Generally, the next number in the sequence is obtained by using this formula:
a_n = a_1 * r**(n – 1)

where a_n = next number in the sequence, a_1 = first number in the sequence, r= common ratio,
n = number of terms

The program should be able to produce output similar to the example below.

Output:
Please enter the initial value a: 2
Please enter the common ratio r: 4

Please enter the number of terms n: 5


2
8
32
128
512

Part 2
Modify the program above to also output the sum of the GP. The sum of the numbers in a GP is
obtained using any of these formulae:
If r = 1, sum = a * n
If r != 1 and r >1, sum = a((rn - 1)/ (r – 1))
If r != 1 and r <1, sum = a((1 – rn )/ (1 - r))

The program should be able to produce output similar to the example below.

Output:
Please enter the initial value a: 1

Please enter the common ratio r: -2

Please enter the number of terms n: 4


1
-2
4
-8
The sum of the GP is -5.0
Optional Exercise 3 - Create a Python program that uses a function

Write a Python program that outputs the pyramid of numbers using nested loops.
The program should use a function that can output any pyramid when given any integer and have
an output similar to the one below.
Output:
Enter the number of rows 8

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8

You might also like