Topic 3.12 (3.A):

  1. Define procedure and parameter in your own words
    • Procedure: aka functions/methods are basically instructions that can store parameters and return values
    • Parameter: variables used in procedure
  2. Paste a screenshot of completion of the quiz

  1. Define Return Values and Output Parameters in your own words
    • Return Values: does a function and tells python to finish the procedure and return a certain value
    • Output Parameters: variables used in procedure (same thing as function parameters)
  2. Code a procedure that finds the square root of any given number. (make sure to call and return the function)
import math
# make function
def root(x):
    result = math. sqrt(x)
    return result
# get result and print
result = root(9)
print(result)
3.0

Topic 3.13 (3.B):

  1. Explain, in your own words, why abstracting away your program logic into separate, modular functions is effective
    • it makes your code more concise, and able to do more things
  2. Create a procedure that uses other sub-procedures (other functions) within it and explain why the abstraction was needed (conciseness, shared behavior, etc.)
    • goal: (x+1)(x-2)
    • I can use subprocedures for this to get the product of x+1 and x-2 becuse I need to do three calculations to get the result
    • inputed 9
x = int(input())
def add(a):
    return a + 1
def multiply(a):
    return a - 2
def equation(x):
    addX = add(x)
    multiplyY = multiply(x)
    return addX * multiplyY

result = equation(x)
print(result)
70
  1. Add another layer of abstraction to the word counter program (HINT: create a function that can count the number of words starting with ANY character in a given string -- how can we leverage parameters for this?)
x = input()
# this function takes a string as input and returns a list of words, where each word
# is a separate element in the list
def split_string(s):
    # use the split() method to split the string into a list of words
    words = s.split(" ")

	# initialize a new list to hold all non-empty strings
    new_words = []
    for word in words:
        if word != "":
            # add all non-empty substrings of `words` to `new_words`
            new_words.append(word)
    
    return words

# this function takes a list of words as input and returns the number of words
# that start with the given letter (case-insensitive)
def count_words_starting_with_letter(words, letter):
    count = 0
    
    # loop through the list of words and check if each word starts with the given letter
    for word in words:
        # use the lower() method to make the comparison case-insensitive
        if word.lower().startswith(letter):
            count += 1
    
    return count

# this function takes a string as input and returns the number of words that start with 'a'
def count_words_starting_with_x_in_string(s):
    # use the split_string() function to split the input string into a list of words
    words = split_string(s)
    
    # use the count_words_starting_with_letter() function to count the number of words
    # that start with 'a' in the list of words
    # set the value of the variable x as the letter the function is looking for
    count = count_words_starting_with_letter(words, x)
    
    return count

# example usage:
s = " hello claire "
x_count = count_words_starting_with_x_in_string(s)
# change to be words starting with whatever letter is inputed
print("Words starting with", x, ":", x_count)
Words starting with h : 1

Topic 3.13 (3.C):

  1. Define procedure names and arguments in your own words.
    • Procedure Names: the name of the function (useful so you can call the function later)
    • Arguments: provides information to a function from outside that function
  2. Code some procedures that use arguments and parameters with Javascript and HTML (make sure they are interactive on your hacks page, allowing the user to input numbers and click a button to produce an output)
    • Add two numbers
    • Subtract two numbers
    • Multiply two numbers
    • Divide two numbers
1st Number :
2nd Number:

Result: