Running Our Code

Programming Foundations with Python >> Lessons >> Use Classes: Send Text >> Running Our Code

In order to get this code to pass, some additional edits not mentioned in the video lesson need to be made that stem from an updated Twilio page. Here is an example of the before and after:

Before Edits:

from twilio.rest import TwilioRestClient

account_sid = "{{ account_sid }}" # Your Account SID from www.twilio.com/console
auth_token  = "{{ auth_token }}"  # Your Auth Token from www.twilio.com/console

client = TwilioRestClient(account_sid, auth_token)

message = client.messages.create(body="Hello from Python",
    to="+12345678901",    # Replace with your phone number
    from_="+12345678901") # Replace with your Twilio number

print(message.sid)

After Edits:

from twilio.rest import TwilioRestClient

account_sid = "account_sid" # Your Account SID from www.twilio.com/console
auth_token  = "auth_token"  # Your Auth Token from www.twilio.com/console

client = TwilioRestClient(account_sid, auth_token)

message = client.messages.create(body="Hello from Python",
    to="+12345678901",    # Replace with your phone number
    from_="+12345678901") # Replace with your Twilio number

print message.sid

Note that your account_sid and auth_token should be replaced by a long combo of letters and numbers copy pasted from your Twilio account, and both phone numbers should be replaced with your own phone number and the phone number given to you by Twilio.

Improving Code Quality

Programming Foundations with Python >> Lessons >> Use Classes: Draw Turtles >> Quiz: Improving Code Quality

Notes on Improving Code Quality

I broke this lesson down into steps to help us produce cleaner code:

Here is the code we start with:

import turtle
    
def draw_square():
    window = turtle.Screen()
    window.bgcolor("red")

    brad = turtle.Turtle()
    brad.shape("turtle")
    brad.color("yellow")
    brad.speed(2)

    brad.forward(100)
    brad.right(90)
    brad.forward(100)
    brad.right(90)
    brad.forward(100)
    brad.right(90)
    brad.forward(100)
    brad.right(90)

    angie = turtle.Turtle()
    angie.shape("arrow")
    angie.color("blue")
    angie.circle(100)

    window.exitonclick()

draw_square()

Problem #1

Step 1: Let’s move this repetitive series of turns into its own, new function.

Step 2: brad the turtle will be drawing a square using this function in another part of our code. This means you will need to add a single parameter to your new function. Label it with a parameter name that would allow us to pass in brad, or a different turtle’s name in case we tell some other turtle to make a square.

Step 2: Notice that this

    brad.forward(100)
    brad.right(90)
    brad.forward(100)
    brad.right(90)
    brad.forward(100)
    brad.right(90)
    brad.forward(100)
    brad.right(90)

is really just this

    brad.right(90)
    brad.forward(100)

repeated 3X. When lines of code repeat, we call it looping. Yes, it is iterating over 3X. So we need an iterator-based for loop. Remember that the directions repeat 3X.

Problem #2

Step 1:

def draw_square():

includes both code for drawing a square and a circle. Change this function name so it makes sense to include turtles that draw squares, circles, triangles, whatever.

Problem #3

Step 1: Since we summed up

    brad.forward(100)
    brad.right(90)
    brad.forward(100)
    brad.right(90)
    brad.forward(100)
    brad.right(90)
    brad.forward(100)
    brad.right(90)

into a new function, you will need to replace the old code

    brad.forward(100)
    brad.right(90)
    brad.forward(100)
    brad.right(90)
    brad.forward(100)
    brad.right(90)
    brad.forward(100)
    brad.right(90)

by calling the new function and passing in the turtle (the name of the turtle) who we want to make us a square.

PS. We don’t need to worry about calling any functions on “angie” the turtle because

    angie.circle(100)

is like

    brad.forward(100)
    brad.right(90)
    brad.forward(100)
    brad.right(90)
    brad.forward(100)
    brad.right(90)
    brad.forward(100)
    brad.right(90)

which is basically

the_turtle_we_want_to_give_directions_to.a_specific_direction(the_parameters_needed_for_that_specific_direction)

 

You can scroll down to see an outline to help you further without giving away all the answers, then if you must, scroll a bit further to see the actual answer, but give it your best shot first!

 

 

 

 

 

 

 

 

 

 

Outline

def draw_square(__________):
    for i in range(_, _)
        __________.forward(100)
        __________.right(90)

def __________():
    window = turtle.Screen()
    window.bgcolor("red")

#Below this comment we create the turtle brad who draws a square
    brad = turtle.Turtle()
    brad.shape("turtle")
    brad.color("yellow")
    brad.speed(2)
    #Below this comment, call our function that draws a square, and pass in the name of turtle you want to draw that square
    _____________(______)

#Below this comment we create the turtle angie who draws a circle
    angie = turtle.Turtle()
    angie.shape("arrow")
    angie.color("blue")
    angie.circle(100)

    window.exitonclick()

draw()

 

 

 

 

 

 

 

 

 

 

 

Solution

def draw_square(some_turtle):
    for i in range(1,5)
        some_turtle.forward(100)
        some_turtle.right(90)

def draw():
    window = turtle.Screen()
    window.bgcolor("red")

#Create the turtle Brad - Draws a square
    brad = turtle.Turtle()
    brad.shape("turtle")
    brad.color("yellow")
    brad.speed(2)
    draw_square(brad)
#Create the turtle Angie - Draws a circle
    angie = turtle.Turtle()
    angie.shape("arrow")
    angie.color("blue")
    angie.circle(100)

    window.exitonclick()

draw_square_then_circle()

Squashing the Bug

Programming Foundations with Python >> Lessons >> Use Functions >> Squashing the Bug

Notes on Squashing the Bug

  1. At the end of the video, he mentions to include the code I show below in bold to further edit the code to include printing the old file_name and the new file_name. I found that after adding this code and running the program, instead of printing an old file_name and a new file_name, you see the new file_name printed twice for each file_name. I think this is because by running the program without this code first, we already changed each file_name within /prank. So the old file_name now IS the new file_name, because, well, we changed it.

IDE input:

     import os
def rename_files():
     #(1) get file names from a folder
     file_list = os.listdir(r"/Users/africansalami/Downloads/Udacity Full Stack/prank")
     #print(file_list)
     saved_path = os.getcwd()
     print("Current working directory is "+saved_path)
     os.chdir(r"/Users/africansalami/Downloads/Udacity Full Stack/prank")
     #(2) for each file, rename filename
     for file_name in file_list:
          print("Old Name - "+file_name)
           print("New Name - "+file_name.translate(None, "0123456789"))
     os.rename(file_name, file_name.translate(None, "0123456789"))
os.chdir(saved_path)

rename_files()

 

SHELL Output

Old Name - .DS_Store
New Name - .DS_Store
Old Name - athens.jpg
New Name - athens.jpg
Old Name - austin.jpg
New Name - austin.jpg
Old Name - bangalore.jpg
New Name - bangalore.jpg
Old Name - barcelona.jpg
New Name - barcelona.jpg
Old Name - beijing.jpg
New Name - beijing.jpg
...

What is the Error?

Programming Foundations with Python >> Lessons >> Use Functions >> What is the Error?

Notes on Entire Course

  1. Download the Google Chrome Add on Youtube Playback Speed and control the speed of videos in real time by increments of .25 seconds by simply clicking the up arrow or down arrow (you can assign which keys you prefer to use). Hint: play this video specifically at .50 for pure entertainment

Notes on What is the Error?

  1. os.rename(src, dst) is what the answer was for Checking OS Documentation, but the answer the teacher uses is actually os.renames(old, new). Maybe they are essentially the same?  If you know, please comment the answer at the end of the post so I can add it here!