Skip to content

Instantly share code, notes, and snippets.

@vivekkalyan
Forked from randName/DW_Guide.md
Last active April 22, 2017 08:43
Show Gist options
  • Save vivekkalyan/2b8b03a681ea54c3acb89c7ec83f306a to your computer and use it in GitHub Desktop.
Save vivekkalyan/2b8b03a681ea54c3acb89c7ec83f306a to your computer and use it in GitHub Desktop.
Guide to the Digital World Midterm

Return

Understand what return does.

def add(x,y):
  return x+y

print add(5,3) + 2 # prints 10

return basically replaces a function call with the value that is returned by the function. Your computer runs the function until it gets a value from it. Once it does, the purpose of the function is done and your computer stops executing the function. Therefore, any code after the first return statement the computer reaches is ignored.

What does this mean? Where you put return is extremely important. Compare the following two pieces of code.

# adds sum of 0 to n
def sum(n):
    total = 0;
    for i in range(n+1):
        total += i
        return total

print sum(5) #prints 0

The computer executes the function line by line and reaches the return statement at the first iteration of the loop. It immediately exits the program and returns the value of the variable total at that point in the program.

# adds sum of 0 to n
def sum(n):
    total = 0;
    for i in range(n+1):
        total += i
    return total

print sum(5) #prints 15

If you have a for loop, make sure to check where return is placed. 99% of the time, it should be outside the for loop.

Another common mistake is to initialize count/sum/total variables inside the for loop. This resets ur variable everytime the for loop is run.

State Machines

'NoneType' object is not iterable error happens because your code runs through the getNextValues function but state value is never assigned when it returns to the transducer. This means that in your if..else statements there is scenario of input that is not being captured by any of the if..else statements.

One potential fix might to always pair if statements with else statements. This ensures that there will always be a value assigned (up to you to ensure that that value is correct!)

Debugging

This is the most powerful skill in programming that is not being taught in class. Debugging is the art of understanding what your program is doing vs what you expect the program to do. And using print statements allows you to quickly and conveniently check your code.

Here are some guidelines to follow when you test your code:

  1. Think of what you expect the variable you are going to be printing to be before you print it. This forces you to think about your program and its goals.

  2. Treat your code with suspicion. Assume every line is wrong unless proven otherwise. This is a good way to start, by challenging any assumptions that you may have about the code. As you grow more comfortable with programming, you will be better able to make educated guesses about where your code can go wrong.

Looking at an (incorrect) function to return the sum of numbers from 0 to n:

# adds sum of 0 to n
def sum(n):
    total = 0;
    for i in range(n):
        total += i
        return total

print(5)
# prints
# 0 

We see that the result returned is 0, which is obviously not the correct sum. Systematically, lets try to check if each line of code works as we expect. To do that we insert print statements to see if the value of the variables at a particular point in the program is what we expect them to be.

# adds sum of 0 to n
def sum(n):
    total = 0;
    print "total: " + str(total) 
    for i in range(n):
        total += i
        return total

print sum(5) 
# prints 
# total: 0
# 0

We expect total to be 0 after the first line, and indeed this is the answer we get.

# adds sum of 0 to n
def sum(n):
    total = 0;
    for i in range(n):
        print "i: " + str(i) # expect 0,1,2,3,4,5  (we are calling sum(5))
        total += i
        return total

print sum(5) 
# prints 
# i: 0
# 0

This is unexpected behaviour, it only prints i: 0. It is up to you to figure out why your program is executing differently from expected. But we have effectively narrowed down our field of search. Since variables up till the for loop have expected values, it has to be after the for loop. In this case, it's the return which is inside the for loop. Let's fix that.

# adds sum of 0 to n
def sum(n):
    total = 0;
    for i in range(n):
        print "i: " + str(i) # expect 0,1,2,3,4,5 (we are calling sum(5))
        total += i
    return total

print sum(5) 
# prints 
# i: 0
# i: 1
# i: 2
# i: 3
# i: 4
# 10

Again this is unexpected as we expect the return value to be 15, and the for loop to output i: 5. We can guess that the error has to be something to do with the values i takes. i is created by using range(n) and thus, it would be wise to check it.

print range(5) # expect: [0,1,2,3,4,5]
# prints
# [0,1,2,3,4]

Reading the range docs, the stop input is exclusive so it stops at 4. If we fix it, it now works as expected.

# adds sum of 0 to n
def sum(n):
    total = 0;
    for i in range(n+1):
        total += i
    return total

print sum(5) 
# prints 
# 15

This seems long-winded and a slow way of debugging your code, but when you are starting out it can be really helpful. As you grow more comfortable, you will naturally get a hang of figuring out which sections of code you need to test and will be able to debug with speed and efficiency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment