  Bag Of Cows

# Session 1: Basic Python Commands

## Times table program.

This program shows how to use iteration to produce a times table. The first version has the number hard coded, but then version two gets input from the user, stores that in a variable, which is then used in the calculation. The final version has the number of iterations also as a variable. The extension is to make the number of iterations also based on user input, and to accept decimals as well as whole numbers.

Demonstrate building the program in stages to the class. Then give pupils the worksheet which needs the blanks filling in for variable names (page 1). They can then type it up, and once it works, try the extensions.

### Concepts covered:

• Variables
• Data types (casting string to integer)
• User input
• Iteration - for in range

### Resources:

Times tables pupil worksheet document

 Basic times tables (37 times table). ``````# Print the times tables # make a variable for the number # we want the times table of num = 37 # print the times table of that up to 20 for i in range(20): # calculate the answer and put in a variable answer = num * i # print the answer print(i, "times", num, "is", answer) `````` User inputs the number. You will need to explain why int() is needed to cast the user input from a string to a numeric type which we can do sums with. Also fixes the problem with the range(), that the times table was doing from 0 to 19 rater than 1 to 20. ``````# Print the times tables # make a variable for the number # we want the times table of num = int(input("What times table would you like? ")) # ! different # print the times table of that up to 20 for i in range(1,21): # ! different # calculate the answer and put in a variable answer = num * i # print the answer print(i, "times", num, "is", answer) `````` Number of iterations also a variable. ``````# Print the times tables # make a variable for the number # we want the times table of num = int(input("What times table would you like? ")) # make the number to go up to a variable too maximum = 30 # ! different for i in range(1,maximum +1): # ! different # calculate the answer and put in a variable answer = num * i # print the answer print(i, "times", num, "is", answer) `````` Pupil Worksheet solution. ``````# Print the times tables # print a welcome message print("=====================================") print(" Welcome to the times table program ") print("=====================================") # make a variable for the number # we want the times table of num = int(input("What times table would you like? ")) # make the number to go up to a variable too maximum = 30 for i in range(1,maximum +1): # calculate the answer and put in a variable answer = num * i # print the answer print(i, "times", num, "is", answer) `````` Pupil Worksheet solution with extension. ``````# Print the times tables # print a welcome message print("=====================================") print(" Welcome to the times table program ") print("=====================================") # make a variable for the number # we want the times table of num = float(input("What times table would you like? ")) # different float, not int # make the number to go up to a variable too maximum = int(input("What number do you want to go up to? ")) # different for i in range(1,maximum +1): # calculate the answer and put in a variable answer = num * i # print the answer print(i, "times", num, "is", answer) ``````

## Turing Test Program

How could we tell if a computer has become truly intelligent? If you can converse with a computer program and not be able to tell whether it is a computer or a person you are talking to, that program passes the Turing Test for machine intelligence.

This program enganes you in conversation - much like colleagues making small talk at lunch...

This program introduces the use of randomness, and also of lists.

There is a pupil worksheet with some missing items for them to fill in, which they can then use to type up the code.

A possible extension for the very able would be that after asking a question the program could use the array.index() function to get the item number in the list, and then use the array.pop() function to remove it, so that the same question is never asked twice.

### Concepts covered:

• Variables
• Choosing random values
• Lists
• Iteration

### Resources:

Turing Test pupil worksheet document

 This is a plan of the code in comments, showing good practice of using pseudo code to think about a problem. ``````# this program make the computer talk like a person # start by showing an introductory message pretending to # be a real person # ask the user their name # welcome them to the program # start by asking an open ended question # let them type in a response # repeat the next bit several times # choose a random follow up question # let them type in a response # when finished, say goodbye `````` Get the code working so that it asks an open ended question, and then follow up questions (but at this stage always the same one). ``````# this program make the computer talk like a person # start by showing an introductory message pretending to # be a real person print("Hi, my name is Ariel") # ask the user their name name = input("what is your name? ") # welcome them to the program print("Great to meet you", name,"lets have a conversation") # start by asking an open ended question print("What was the most interesting thing that happened to you last week?") # let them type in a response input() # repeat the next bit several times for i in range(4): # choose a follow up question print("That is really interesting, tell me more! ") # let them type in a response input() # when finished, say goodbye print("I have to go now. It was great talking to you", name) `````` Enhance to include a list of follow up questions and randomly pick one each time. ``````# this program make the computer talk like a person import random # start by showing an introductory message pretending to # be a real person print("Hi, my name is Ariel") # ask the user their name name = input("what is your name? ") # welcome them to the program print("Great to meet you", name,"lets have a conversation") # start by asking an open ended question print("What was the best thing that happened yesterday?") # let them type in a response input() # this is a list of possible questions questions = [ "How interesting. And then what?" , "What happened next?" , "How did that make you feel?" , "Is that the first time something like that has happened?" ] # repeat the next bit several times for i in range(4): # choose a follow up question q = random.choice(questions) print(q) # let them type in a response input() # when finished, say goodbye print("I have to go now. It was great talking to you", name) ``````

## Are we there yet? Child personality simulator

This program introduces while loops to iterate when we don't know how many times we want to loop, so we just carry on until some condition is met.

As it progresses, we introduce selection with IF statements, so we can do different things depending on what the user inputs.

### Concepts covered:

• Variables
• Iteration with WHILE
• Selection with IF, ELIF
• Using time.sleep() for delays

### Resources:

Child simulator pupil worksheet document

 A basic while loop where the program loops for ever. ``````# irritating child simulator import time while True: # child asks a question print("Can I have some sweets? ") # you answer input() # pause a little bit before asking again time.sleep(1) `````` Slightly better, as the child will at least stop if we give it what it wants. Uses an IF and then if the condition is met, a break to jump out of the loop. ``````# irritating child simulator import time while True: # child asks a question print("Can I have some sweets? ") # you answer the question answer = input() # ! Different #if the answer is what they want.... if answer == "yes" : # ! New # jump out of the while loop break # ! New time.sleep(1) `````` Building on the last version, this uses ELIF and ELSE to give different responses from the child depending on what the answer was. ``````# irritating child simulator import time while True: # child asks a question print("Can I have some sweets? ") # you answer the question answer = input() #if the answer is what they want.... if answer == "yes" : print("Yay!") # jump out of the while loop break # or if the answer upsets them.... elif answer == "shutup" : # ! New print("waaaaaaa") # anything else .... else: # ! New print("I'm not happy") # pause a little bit before asking again time.sleep(1) ``````