In my approach to keep learning I’ve signed up to Programming 102: Think Like A Computer Scientist” via FutureLearn and the Raspberry Pi Foundation. The course is available for free (or for a small fee to have a certificate) at:
I’m using this blog post to post my code for the course. The course runs for 4 weeks and is estimated to take 2 hours a week.
Week 1
1.7 Functions
Task: “Create a function that asks the user for the height and base of a triangle, and then calculates and prints out its area.”
For the first part of this task I had to remember how to for the area of triangle (Base*Height/2). Then I needed to write it as a function and I called on the function to test it:
A function to find the area of a triangle
def triangle():
height = input(‘Please enter height of triangle: ‘)
base = input(‘Please enter base of triangle: ‘)
height = int(height)
base = int(base)
area = (height*base)/2
print(‘The area of the triangle is’,area)
triangle()
1.8 Functions Including Parameters
Task: “Create a function that accepts a string and returns the same string reversed”.
A function to take a string via user input and reverse it
def reverse_string():
input_string = input(‘Enter some text: ‘)
string_revs = input_string[::-1]
return string_revs
print (reverse_string())
1.9 Functions With Parameters
Task: “At the moment all our circles are drawn at the centre of the screen. Can you add parameters to the draw circle function so that you can draw circles at different locations on the screen?“.
A Turtle Function to draw a circle at X,Y co-ordinates.
from turtle import Turtle
george = Turtle()
def draw_circle(name,r,color,x,y):
name.penup()
name.color(color)
name.goto(x,y)
name.pendown()
name.dot(r*2)
draw_circle(george, 50,”blue”,-100,50)
1.10 Return Values
Task: “Create similar functions to calculate the area of rectangles and use them in your draw_rectangle function to annotate rectangles.”
3 thoughts on “Programming 102 “Think Like A Computer Scientist” (Python / Raspberry Pi)”
Comments are closed.