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:

https://www.futurelearn.com/courses/programming-102-think-like-a-computer-scientist/

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)
triangle()
1.8 Functions Including Parameters
Task: Create a function that accepts a string and returns the same string reversed”.
python_reverse_string_geektechstuff
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):
    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.”
102_1_10_turtle_func_geektechstuff.png
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):
    name.hideturtle()
    name.penup()
    name.goto(x,y)
    name.pendown()
    for x in range(2):
        name.forward(w)
        name.right(90)
        name.forward(l)
        name.right(90)
    name.color(“black”)
    name.write(“Area: “+str(rectangle_area(w,l)),align=’center’)
draw_rectangle(george,50,300,-50,50)

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“.
102_1_13_func_geektechstuff.png
# geektechstuff
## Guessing Game
def guess():
    min=0
    max=101
    # sets the loop count to zero
    x=0
    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
        else:
             correct_guess = True
      return(print(“Your number is”,middle,”it took me”,x,”guess”))
guess()
I’ll be posting more of my work as I complete the tasks.