Touring Turing

1 Looping

Learn It

  • At the moment our scripts have run in one direction - Top to Bottom.
  • Even though we've used branches (with if, elif and else), the branches still run from top to bottom.
  • When Turing devised his Turing Machine he imagined that the machine would be able to move forwards and backwards along the tape.
  • This would mean that the machine could end up reading the same section of tape, multiple times. This would be a loop.

Learn It

  • In Python we can use what is known as a while loop.
  • Thinking back to our earlier examples of deciding what to do depending upon the weather is like outside; you might say something along the lines of…

While it is raining outside, I am staying indoors.

  • In Python this would look something like;
while raining == True:
    print('I am staying indoors')
  • Notice that we need to use that same : on the end of the condition.
  • We also need that significant whitespace before any code that is supposed to run inside the loop.

Code It

  • Let's start by trying a version of the example above, but add a little user input into the code.
raining = input('Is it raining outside? ')
while raining == 'yes':
    print('You had best stay indoors')
print('You can go outside')
  • Feel free to use the Trinket below.
  • Run your code and see what happens, when you type 'no'
  • As raining does not equal 'yes', the computer does not enter the while loop, so the lines are skipped to the next line with no whitespace.
  • Run it again, and this time type 'yes'.
  • Is there a problem?
  • When using the IDLE environment, you can quit a running program by using the shortcut Ctrl+c

Learn It

  • You've just experienced your first infinite loop.
  • This is when a program runs and runs and never ends.
  • The problem was that once we had set the variable raining to 'yes' there was no way to change it. raining was always going to be 'yes.

Code It

  • We can alter our code to allow changing of the variable though.
raining = input('Is it raining outside ')
while raining == 'yes':
    print('You had best stay indoors')
    raining = input('Is it raining outside ')
print('You can go outside')
  • Now run the code and see what happens.

Learn It

  • We can use the same Comparison Operators for a while loop that we used for conditionals.
Comparison Operator Description Example
== Tests if two things are equal 6 == 6 is True
!= Tests if two things are not equal 4 != 6 is True
< Tests if one thing is less than another 3 < 10 is True
> Tests if one thing is greater than another 10 > 3 is True
<= Tests if one thing is less than or equal to another 10 <= 10 is True and 9 <= 10 is True
>= Tests if one thing is greater than or equal to another 5 >= 5 is True and 5 >= 3 is True

Code It

  • We can use a while loop to make a password authentication program.
  • This time we'll use the =! comparison (not equal to)
password = 'correct horse battery staple'

attempt = input('Please enter a password: ')

while attempt != password:
    print('Access denied')
    attempt = input('Please enter a password: ')
print('Access granted')

Badge It - Silver

  • Try and write a script that asks the user to answer a simple sum.
  • If they get it right, they should be congratulated
  • If they get it wrong they should be asked the question again.
  • Don't forget to type cast the user input.
  • The example above is shown below in a Trinket; you might be able to use this to help.

2 Counting

Learn It

  • One of the simplest tasks we can get a computer to do is to count.
  • When you count to 100, you start at 1 and then keep adding on 1 until you get to 100.
  • Let's code this with a while loop.

Code It

count = 1

while count <= 100:
    print(count)
    count = count + 1
  • Can you alter the code so that the computer counts to 10000
  • Alter it so that it counts in 2s, then 10s

Badge It - Gold

  • Create a while loop that counts down from 100 to 0.
  • Create a while loop that prints out 1, and then doubles it, printing out the answer each time until it reaches 1,000,000
  • Create a while loop that prints out Computing is Grrrreat a thousand times.
  • Save a screenshot of your code for these problems in your folder.

3 Singing

Learn It

  • There are lots of counting songs that children use to learn to count:
    • 5 Little ducks went swimming one day
    • 5 Currant buns in a baker's shop
    • 5 Little Speckled Frogs
    • 5 Little Monkeys
    • 5 Red Apples
  • If you don't know the lyrics, you can listen to some of them here.

Code It

  • Writing out the lyrics to a simple counting song wouldn't take too long.
  • You could probably write out the lyrics to 10 Green Bottles fairly quickly
  • But what if I asked you to write out the lyrics to 100 or even 1000 green bottles.
  • Have a look at the script below.
bottles = 1000

while bottles > 0:
    print(str(bottles) + ' green bottles sitting on the wall,')
    print(str(bottles) + ' green bottles sitting on the wall,')
    print('And if one green bottle should accidently fall,')
    bottles = bottles - 1
    print("There'll be " + str(bottles) + ' green bottles, sitting on the wall.')
  • Copy and paste the code into a new script and run it. You can change the bottles variable to anything you like.

Badge It - Platinum

  • Try and create a script to write out the lyrics of one of the songs listed above.
  • Use a variable to keep track of the numbers of Frogs, Buns or Ducks, and a while loop to change them.
  • The code for 100 green bottles is given for you to get you started.

Badge it!