To help you further your skills with Python this worksheet has some difficult challenges. Give them your best shot and ask for help if you need it.
-
1
Write a program that prints numbers from 100 to 1000.
-
2
Write a program that asks the user for a number, then displays the times table (up to 10 times) for the number they have given by adding the number to a total. E.g. if they type 7 it will display:
7 14 21 28 35 42 49 56 63 70The code will contain something like this
total = total + number. -
3
Write the previous program in another way by simply changing the range function in the loop.
-
4
Write a program that displays all even number from 0 to 100. To figure out if a number is even use
if number % 2 == 0:. -
5
The Fibonacci numbers are a sequence of numbers where each successive number is the previous number plus the one before that.
It starts on the two numbers, 0 and 1:
1 (0 + 1) 2 (1 + 1) 3 (2 + 1) 5 (3 + 2) 8 (5 + 3) 13 (8 + 5)
You will have three variables, x and y and a temp variable.
temp = x x = y y = temp + y
You can ask the teacher or look this up if you want a more concrete explanation of this.
a. Write a program that will generate the sequence of numbers above. To do this, you will need:
- i. Two variables: x and y.
- ii. A loop that will loops 10 times
- iii. A temporary variable to store num1 within the loop (e.g. temp = x)
- iv. A method of displaying each number to the screen
b. Write a program that will display the 50th Fibonacci number only.
-
1
Write a program that prints all numbers from 1 to 100 that are divisible by 3 and 5.
-
2
Write a program that asks the user for a number, then counts down from that number to 0.
-
3
Write a program that adds up all numbers from 1 to a number the user types in.
-
4
Write a program that displays the square of every number from 1 to 20.
-
5
Write a program that displays a simple 'bar chart' using stars.:
If the user enters 5, show:
*****
If the user enters 3, show:
***Ask the user how many times they want this to display, e.g. if they say 3 and they want 4 stars it should look like this:
**** **** ****
Challenge tasks
These tasks are much harder and you may need to look up things to help you with this.
-
1
Write a program that asks the user for a word and displays it backwards.
-
2
Write a program that asks the user for 5 numbers and displays the largest one. (You will need a variable that stores the current largest number seen so far.)
-
3
Display all prime numbers between 1 and 100.