Worksheet 1#
Exercise (For loop)
Level:
Take the code used to do Exercise 8 but this time, for each iteration, print the index of the word in list l
next to the string.
Solution to Exercise
l = ["keep", "calm", "and", "carry", "on"]
for i in range(len(l)):
print(i, l[i])
0 keep
1 calm
2 and
3 carry
4 on
Exercise (Odd and even numbers)
Level:
Modify the code that identifies an odd or even number to create a list of even numbers, and another list of odd numbers.
Solution to Exercise
#initialise empty lists
even, odd = [], []
for i in range(10):
if i % 2 == 0:
even.append(i)
continue
odd.append(i)
print(f"The program has identified {len(even)} even numbers {even}")
print(f"The program has identified {len(odd)} odd numbers {odd}")
The program has identified 5 even numbers [0, 2, 4, 6, 8]
The program has identified 5 odd numbers [1, 3, 5, 7, 9]
Exercise (Simple calculator)
Level:
Creat a program that works like a simple calculator:
First, print a user menu that shows the operations available with this calculator: 1. Addition
2. Subtraction
3. ExitThen ask the user to enter two numbers.
The calculutaor would then add or subtract these two numbers depending on the operation selected previously.
Tip
The input()
function is used to read in the user’s input from the console.
Solution to Exercise
while True: #1
print("1. Addition") #2
print("2. Subtraction") #3
print("3. Exit") #4
print("Choose operation (1-3): ", end="") #5
opt = int(input()) #6
if opt>=1 and opt<=2: #7
num1 = float(input("Enter first number:")) #8
num2 = float(input("Enter second number:")) #9
if opt==1: #10
print("Result:", num1 + num2) #11
elif opt==2: #12
print("Result:", num1 - num2) #13
elif opt==3: #14
break #15
else: #16
print("No option available") #17
print() #18
#19
print("Thank you, bye!") #20
The first line is an infinite loop which means that the loop will iterate forever since
True
is specified instead of a condition. This will not be the case however, as in the if
statement we have a break
in line 15, which
stops the loop if the user chooses 3 as an option in the calculator. Note again the indentation of the code. The last line is not
indented under the loop, so it will run after execution of the loop has finished.