Cyborg

Code world 201

Living in a world where the separation between man and computer is increasingly blurry.

 

Learning to code makes us masters of technology.

What we are doing today

Last class, we explored the fundamentals of coding.

 

First we made computers speak.

Then we taught it to remember things. 

We're going to kick it up a notch and create our first human/computer conversation. By opening the lines of communication, we can teach computers to interact with us.

 

Today, we are going to teach computers to listen. 

Mother and Daughter Communicating

Exercises: Review

  1. Copy and paste this code into your Replit.

    print('Hello, world!')

    This is the print() statement.
     

  2. Make the computer print these lines of text.

    What did the fish say when he ran into a wall?
    Dam.
     

  3. Predict what each print() statement will display.

    president = "Abe Lincoln" 
    print(president)
    print("president")

     

  4. Combine these two values then print them. It should print "260".

    distance_to_barstow = 120
    distance_to_vegas = 140

How do computers listen?

When computers are taught to listen, they can do amazing things for us.

 

  • They can scour the internet for us.

  • They can summon a cab to our front door.

And it all starts from a single line of code.

instagram.png

Exercises: Information in / Information out

  1. Copy and paste this code into your Replit.
    name = input("Favorite singer: ")
    print(name)


    input() sends whatever the user typed into the console into the variable name. Your console should look like this:

    Favorite singer: selena gomez
    selena gomez


    We will mark the input label (e.g. "Favorite singer") in green. 
     

  2. Your turn! Create this:

    How old are you: 51
    51
     

  3. Fix the bug! The code below doesn't work. 

    temp = input("Current temperature: ")
    print(temperature)


    Fix the problem so that it does this:
    Current temperature: 84
    84
     

  4. Full name generator. Notice it prints the full name twice!

    First name: Taylor
    Last name:
     Swift
    Taylor Swift
    Taylor Swift

     

  5. Code prediction! Predict what this code will do before you run it.

    print("Sum it up!")
    first_addend = input("First number: ")
    second_addend = input("Second number: ")
    total = first_addend + second_addend
    print(total)


    * This code acts funny. Don't try to fix it! We'll show you how to fix it later.

It's a big world after all

Just like how there are different types of vegetables at the farmer's market, there are different types of data.

 

The two we are going to use today are Strings and Integers:

 

  1. Strings are for text.
    "Hello World!"
     

  2. Integers are for numbers.
    123123

Fruit Market

Quiz: Name the data type!

  1. theater = "AMC theater"
     

  2. height = 52
     
  3. weight = "251 lbs"
     
  4. speed = "85"

Changing data types

In our last exercise, our app behaved like this:

Sum it up!
First number: 12
Second number: 34
1234


What happened was there are two ways to add variables, as a String or as an Integer. Our code was adding numbers as if they were Strings! 

Fortunately, just like how water can be turned into steam, data types can be changed as well. If we do this right, we can make our Strings add as Integers!

Sum it up!
First number: 12
Second number: 34
46

Hot Sauna

Exercises: Data Types

  1. Compare the two! Take a look at the old code.

    print("Sum it up!")
    first_addend = input("First number: ")
    second_addend = input("Second number: ")
    total = first_addend + second_addend
    print(total)

    Anytime a value comes through input(), it comes in as a string. To fix our problem, we need to cast the value from a String into an Integer.

    print("Sum it up!")
    first_addend = input("First number: ")
    second_addend = input("Second number: ")
    total = int(first_addend) + int(second_addend)
    print(total)
     

  2. Bill calculator. Add your bill together.

    Total cost: 49
    Taxes: 4
    53

     

  3. Fix the bug! This code doesn't work. Help me fix it!

    sarah = "186"
    sally = "159"
    total = int(sarah_score + sally_score)
    print(total)

     

  4. Restaurant calculator. Calculate everyone's fair share!

    Food bill: 20
    Number of people: 4
    5

     

  5. Fix the bug! This code doesn't work. Help me fix it!

    age = int(current_year - birth_year)
    birth_year = input("What year were you born? ")
    current_year = input("What year is it? ")
    print(age)

     

  6. Miles calculator. Calculate how many miles we got left in the tank.

    Gallons left in tank: 7
    Miles per gallon: 25
    175