Worksheet 1
Worksheet 1#
Exercise (For loop)
Level:
Take the code used to do Exercise 1.11 but this time, for each iteration, print the index of the word in list l
next to the string.
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.
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.
Exercise (Asterisk half-pyramid)
Level:
Draw a half pyramid as shown below using asterisks.
*
* *
* * *
* * * *
* * * * *
Tip
Use nested loops: one loop inside another, with an outer loop to handle rows and an inner loop to handle columns.
Exercise (Asterisk full-pyramid)
Level:
Draw a full pyramid using asterisks.
Tip
Use nested loops as in half-pyramid
and think about the number of spaces and asterisk you need in each row as shown below: