Touring Turing

1 More Conditionals

Learn It

  • There's actually a lot more that we can do with Conditional Selection.
  • There is another keyword called elif that means else if
  • This is useful if we are testing for multiple conditions.
  • In lesson 3 we used an example of it being cold outside. But cold or not cold is a binary choice, meaning there are only two choices.
  • We could have a range of descriptions of the temperature outside - cold, mild, warm or hot for example.
  • if it's cold you'll need a coat, else if it's mild you go out, else if it's warm you take off your jumper, else you put on some shorts.

Code It

  • Let's try and code that up.
  • Create a new script called weatherChecker.py
temperature = input('What is it like outside? ')
if temperature == 'cold':
    print('Better take a coat')
elif temperature == 'mild':
    print('You can just go out')
elif temperature == 'warm':
    print('You had better take off your jumper')
else:
    print('You had better put on some shorts')

Try It

  • Can you extend the script for more temperature options - freezing, chilly, tropical for instance?
  • Try adding on a another block of conditional selection that asks about the time of day - morning, afternoon, evening perhaps, and advises on whether the user has eaten their breakfast, lunch or tea before they go out.

Badge It - Silver

  • Choose 4 friends in the class.
  • Create a script that asks for the user to type in their name.
  • If the name provided matches one of your friend's names then it should give a nice response.
  • If it does not match your friend's names then it should produce a mild insult.
  • The code from above is given to help you get started…

2 More Operators

Learn It

  • You already know that a double equals is used to test for equality, whether something is equal or not.
  • There are other tests we can perform though.
  • Look at the table below.
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

  • So how can we use this?
  • Let's try and make a little script that insults people based on their age.
  • Make a new script (Ctrl+n) and call it ageist.py
age = int(input('How old are you? '))
if age <= 10:
    print('You are just a tiny baby.')
elif age <= 12:
    print('You are cool')
elif age <= 30:
    print('You are getting on a bit')
else:
    print('Oh no, sorry but you are ancient')

Badge It - Gold

  1. Write a script that asks the user what year Alan Turing died. If they get it right, it should congratulate them. If they're too early or late, it should tell them accordingly.
  2. Write a script that asks the user for the to calculate the sum of two numbers, and tells them if they're too high, too low or correct.
  3. Write a script that asks the user where Alan Turing worked during World War II. If they don't get it right it should tell them so. Otherwise it should congratulate them.

3 Nesting Conditionals

Learn It

  • Sometimes in our scripts, we want to be able to make multiple branches.
  • If an outcome depends on two different conditions we can nest our if statements.

Code It

  • The script below makes a start at producing an automatic horoscope predictor.
  • Your star sign depends on both the month and day you were born, so we need to use nested if statements.
month = input('What month were you born in? ')
day = int(input('What day of the month were you born? '))
if month == 'January':
    if day > 19:
        print('You are an Aquarius')
        print('You will live a long life')
    else:
        print('You are a Capricorn')
        print('You will live a long life')
elif month == 'February':
    if day > 19:
        print('You are a Pisces')
        print('You will live a long life')
    else:
        print('You are an Aquarius')
        print('You will live a long life')
  • Finish of the script so that it works for any day of the year.

- This website might help

Learn It

  • There's no reason why you can't have further nesting if you need it.
  • However, the more nesting you have, the more difficult your script will be to read and debug if you have an error.

Badge It - Platinum

  • The lessons you go to each day are dependent on three variables.
    • The Current Week (Week 1 or Week 2)
    • The Current Day (Monday - Friday)
    • The Current Period (1-5)
  • Create a script that will report back which lesson you should be in, dependent on how you answer three questions regarding the Week, Day and Period.