Hack 1

  • Procedure: a procedure is like a function (it carries out an action)
  • Parameter: a parameter is a variable that holds data from previously in the function
  • Return Values: return values are what is returned after the "return" statement <- they allow for the data to be refrenced later in the code (ex: print)
  • Output Parameters: the parameters that refrence the output of a previous function
questionNum = 3
correct = 0
questions = [
    "What is are correct names for a procedure? \n A) Method \n B) Function \n C) Both",
    "What is a procedure? \n A) Sequencing \n B) Selection \n C) Iteration \n D) All",
    "Use this for following question: \n def inchesToFeet(lengthInches): \n\t lengthFeet = lengthInches / 12 \n\t return lengthFeet \n\n What is the procedure name, the parameter, and what the procedure returns? \n A) feetToInches, lengthInches, lengthMeters \n B) inchesToFeet, lengthInches, lengthFeet \n C) inchesToFeet, lengthFeet, lengthInches \n D) lengthInches, inchesToFeet, lengthFeet"]
answers = ["c", "d", "b"]

def qna(question, answer):
    print("Question:", question)
    response = input()
    print("Answer:", response)
    
    if response.lower() == answer:
        print("Correct :) \n")
        global correct
        correct += 1
    else:
        print("Incorrect :( \n")
for x in range(questionNum):
    qna(questions[x], answers[x])
    
print("Score:", correct, "/ 3")
Question: What is are correct names for a procedure? 
 A) Method 
 B) Function 
 C) Both
Answer: c
Correct :) 

Question: What is a procedure? 
 A) Sequencing 
 B) Selection 
 C) Iteration 
 D) All
Answer: d
Correct :) 

Question: Use this for following question: 
 def inchesToFeet(lengthInches): 
	 lengthFeet = lengthInches / 12 
	 return lengthFeet 

 What is the procedure name, the parameter, and what the procedure returns? 
 A) feetToInches, lengthInches, lengthMeters 
 B) inchesToFeet, lengthInches, lengthFeet 
 C) inchesToFeet, lengthFeet, lengthInches 
 D) lengthInches, inchesToFeet, lengthFeet
Answer: b
Correct :) 

Score: 3 / 3
import math
number = input("What number would you like to take the square root of?")
def square_root_function(number):
    square_root = math.sqrt(int(number))
    return square_root
print("The square root of " + number + " is " + str(square_root_function(number)))
The square root of 4 is 2.0

Hack 2

  1. Explain, in your own words, why abstracting away your program logic into separate, modular functions is effective:
    • abstracting away your program logic into seperate functions is effective since it allows for you to be more time efficicent, and make fewer errors. If you need to identify/change an error, you have less to work with and change. Also, it is easier to follow, and can reference pre-existing functions (making the coding simpler)
# This code simulates the scoring at a cheerleading competition <- the overall score for the comp is 25% of your first performance (day 1) and 75% of your second performance (day 2)
scores_given = ["Code 5", "Coed"]
team_name = input("What is the name of your team?")

def combined_score(score1, score2): #defining this function makes it much simpiler when trying to score hundreds of teams
    # instead of writing all of this code again for each team, it is used repeatedly
    combined_score = ((0.25*score1)+(0.75*score2))
    print(team_name,"'s overall score for the competition is ", combined_score)
    scores_given.append(team_name)
   

combined_score(score1 = int(input("what was your day 1 score?")), score2 = int(input("what was you day 2 score?")))
print("Scores have been given to ", scores_given)
TGLC 's overall score for the competition is  97.25
Scores have been given to  ['Code 5', 'Coed', 'TGLC']
# 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_a_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
    count = count_words_starting_with_letter(words, "a")
    
    return count

# see above
def count_words_starting_with_d_in_string(s):
    words = split_string(s)
    count = count_words_starting_with_letter(words, "d")
    return count

       
    # this function takes a string as input and returns the number of words that start with any inputted letter
def count_words_starting_with_l_letter(s):
    l = input("What letter would you like to check?")
    words = split_string(s)
    count = count_words_starting_with_letter(words, l)
    return count


# example usage:
s = "   This is  a  test  string! Don't you think this is cool? "
l_count = count_words_starting_with_l_letter(s)
a_count = count_words_starting_with_a_in_string(s)
d_count = count_words_starting_with_d_in_string(s)
print("Words starting with your letter:", l_count)
Words starting with your letter: 1

Hack 3

  • procedure names: the name of the function in which you are calling
  • arguments: values that are sent to a function when called for