Programming 102 “Think Like A Computer Scientist” (Python / Raspberry Pi)

A function to find the area of a triangle

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
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)
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.
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):
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.”
from turtle import Turtle
george = Turtle()
def rectangle_area(w,l):
    # w for width, l for length
    return w*l
def draw_rectangle(name,w,l,x,y):
    for x in range(2):
    name.write(“Area: “+str(rectangle_area(w,l)),align=’center’)

1.13 Improve A Piece Of Code With Functions
Task:”Provided a short-ish program that currently contains no functions. We’d like you to suggest functions that could be added to the program“.
# geektechstuff
## Guessing Game
def guess():
    # sets the loop count to zero
    correct_guess = False
    print(“Think of a number between 0 and 101”)
    # loops until program guesses correctly
    while correct_guess ==False:
        # increases the loop count by one
        x = x+1
        middle = int((max + min)/2)
        answer = input(“Is your number [H]igher, [L]ower or the [S]ame as {}”.format(middle)).upper()
        if answer ==”H”:
            min= middle
        elif answer ==”L”:
            max= middle
             correct_guess = True
      return(print(“Your number is”,middle,”it took me”,x,”guess”))
I’ll be posting more of my work as I complete the tasks.