Programming in English Schools
From 3-18
Miles Berry
@mberry
These slides: bit.ly/smartatbett
24 January 2018
qbmaze
Computing
A high-quality computing education equips pupils to use computational thinking and creativity to understand and change the world.
programming = algorithms + code
Before 5
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!")
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")
Discussion...
@mberry
m.berry@roehampton.ac.uk
milesberry.net
These slides: bit.ly/smartatbett