Touring Turing

1 Scripting in Python

Learn It

  • Up until now you have been coding in the INTERPRETER
  • When Turing came up with the concept of his Turing Machine he imagined that cards would be used where instructions could be written for the machine to act upon, depending upon what was written on the tape.
  • We want to use the Python equivalent of these cards.
  • We call these programs or scripts

Try It

  • Find and open Thonny again.
  • Now using your keyboard, hit Ctrl + n to make a new file.
  • A tab should open up in the code editor windoq that looks like this.

thonny_newfile.png

  • At the moment it is Untitled so we had best give it a name.
  • Hit Ctrl + s on your keyboard to save the file.
  • Give it the name firstScript.py
  • Make sure you save it in your own home drive, in your computer science folder.
  • The .py file extension is important. It lets the Operating System know that the file needs to be run with Python, and not another application like Notepad.
  • With the .py extension, you'll also get help with the language syntax as key words will be coloured.
  • Thonny allows you to work with multiple Python files by keeping them in tabs.

Code It

  • Let's write a simple script in our new window. If you see the keyword SCRIPT in this resource you know you should be writing in this window. If you see the keyword INTERPRETER you know you should be writing in the interpreter window.
foo = 10
bar = 12
baz = foo + bar
  • You need to save your file again Ctrl + s and then use the F5 key to run the program.

Learn It

  • You'll have noticed that nothing appears to happen.
  • Your program has actually run though, and your variables are stored in RAM.
  • Flick over into your INTERPRETER and try querying the variables.
foo
bar
baz
  • You'll see the values displayed in the interpreter.

2 Printing

Code It

  • It's going to be annoying if we have to constantly jump into the interpreter to see what the values of our variables are.
  • To display data, most programming languages have a method of outputting data. In Python we use an inbuilt function called print().
  • Make a second script called secondScript.py (Ctrl+n then Ctrl+s)
  • Write this in your SCRIPT
print(Hello World!)
  • Run the script and see what happens.
  • We get a syntax error because the Python Interpreter thinks that Hello is some sort of variable that we haven't created.
  • We need to make sure that it realises we're either printing strings, numbers or the values of variables
  • Try this instead
print('Hello World!')
  • Now we've put quotes around the words, we can run our script.

Learn It

  • We can print almost anything we like. Try out the following little scripts.

Badge It - Silver

  • Using only the variables provided in each of the scripts below, create print statements that produce the displayed output.

Set A

foo = 10
bar = 5
baz = 12

Output 1

50

Output 2

6

Output 3

238

Set B

foo = 'Ba '
bar = 'Black Sheep '
baz = 10

Output 1

Ba Ba

Output 2

Ba Ba Black Sheep

Output 3

Ba Ba Black Sheep Ba Ba Black Sheep Ba Ba Black Sheep Ba Ba Black Sheep Ba Ba Black Sheep Ba Ba Black Sheep Ba Ba Black Sheep Ba Ba Black Sheep Ba Ba Black Sheep Ba Ba Black Sheep

3 Getting Input

Learn It

  • Now that you've got the idea of printing, let's have a look at user input.
  • Sometime we'll want the user of our program to be able to set variable values. This is handled using the inbuilt input() function.
  • Here's an example of the use of input()
name = input('What is your name?)
  • It is made up of three parts
    1. The variable identifier. This is how the user input is stored.
    2. The function's name. This is always input.
    3. A prompt. This can be any string you like.

input.png

Code It

  • Let's try a few uses of input()
  • We'll make a simple 6 line script.
  • Computers are pretty strict about the order in which they execute instructions, so they'll always start at the first line and run the lines in order.
name = input('What is your name? ')
print('Hi there ' + name)
pet = input('What type of pet do you have? ')
print('Oh, I love ' + pet + 's')
place = input('Where do you live? ')
print("I've heard that " + place + ' is a horrible place to have ' + pet + 's')

Badge It - Gold

  • Create a new script called conversation.py.
  • Try to write your own script that let's you have a bit of a conversation with the computer.
  • Try to use at least three or four input() and print() statements.

Code It

  • Sometimes we want the user to enter numbers instead of strings.
  • With number we can do some calculations.
  • Let's write some code that doesn't quite work.
  • Create a new script called Calc.py
print('I am the Amazing Calculator, that can add any two numbers in an instant')
firstNum = input('Give me my first number puny mortal ')
secondNum = input('Now give me my second number, you fool ')
answer = firstNum + secondNum
print('The answer to your easy question is ' + answer)
  • Run your code and see what happens.

4 Converting Types

Learn It

  • The Amazing Calculator messed up the calculations
  • So what went wrong.
  • input() always turns what ever the user types in, into a string.
  • Just try the following in your INTERPRETER
10 + 10
'10' + '10'
  • When you tell the computer to add integers, it adds them together as you would expect.
  • If you tell the computer to add together strings (like '10') it concatenates them. This is a fancy word for joining together.
  • Now say the phrase concatenation means joining together twenty times to yourself, because it's going to come up a lot in the future.

Code It

  • What we need to do is find a way to convert our strings to integers.
  • When we convert one type to another, it is called Type Casting
  • This is easy to do with some more inbuilt functions. In this case we're going to use the int() inbuilt function.
  • Try the following in the INTERPRETER
int('10') + int('10')
  • Using int() we convert the string to an integer.
  • We can easily convert variables as well.
foo = '10'
bar = '5'
int(foo) + int(bar)
  • Using this we can convert a users input, before it is even stored as a variable value.
num = int(input('Give me a number '))
  • If we want to concatenate numbers onto string we have a problem though.
answer = 5
print('The answer is ' + answer)
  • If you try to run this, you'll get an error as we can't concatenate strings and integers.
  • We need to type cast the int to a str first.
answer = 5
print('The answer is ' + str(answer))

Badge It - Platinum

  • Use what you have learned to fix the 'Amazing Calculator'
  • You'll need to type cast the user input to ints, then type cast the answer back to a string.
  • Can you make another script that asks the user for 5 numbers and calculates the mean average of the numbers?