1 of 19

Programming in English Schools

From 3-18

Miles Berry

@mberry

These slides: bit.ly/smartatbett

24 January 2018

2 of 19

qbmaze

3 of 19

Computing

A high-quality computing education equips pupils to use computational thinking and creativity to understand and change the world.

4 of 19

programming = algorithms + code

5 of 19

Before 5

6 of 19

7 of 19

8 of 19

9 of 19

10 of 19

11 of 19

12 of 19

13 of 19

from random import randint

secret = randint(0, 127)

print("I'm thinking of a number between 0 and 127")

guess = -1

while guess != secret:

guess = int(input("What's your guess? "))

if guess < secret:

print("Too low!")

elif guess > secret:

print("Too high!")

print("Yay!")

14 of 19

15 of 19

16 of 19

A solution

import random

num='0123456789'

code=''.join(random.sample(num,4))

attempts=0

def compare(guess,code):

bulls=0

cows=0

remaining_x=[]

remaining_y=[]

for x,y in zip(guess,code):

if x==y:

bulls+=1

else:

remaining_x.append(x)

remaining_y.append(y)

for x in remaining_x:

if x in remaining_y:

cows+=1

remaining_y.remove(x)

return (bulls,cows)

def validate(guess):

if len(guess) != 4:

print("Wrong length")

return False

for i in range(4):

if guess[i] not in num:

print("Not a number")

return False

if guess[i] in guess[i+1:3]:

print("Contains duplicates")

return False

return True

while True:

guess=input('Enter any four digits: ')

if guess == "exit":

print('The code was %s' % code)

break

if validate(guess):

attempts += 1

(bulls,cows)=compare(guess,code)

if bulls == 4:

print('Success, you cracked the code in %d attempts' % attempts)

break

else:

print('You have %d bulls and %d cows' % (bulls, cows))

else:

print("Guess again")

17 of 19

18 of 19

19 of 19

Discussion...

@mberry

m.berry@roehampton.ac.uk

milesberry.net

These slides: bit.ly/smartatbett