3.12 Hacks

Procedure:
  • A procedure is a piece of code that is made to have accomplish a certain goal (like a function)
Parameter:
  • A parameter is a variable that allows data to be imported into a function.
    • A parameter can look like "a" and "b" in random.randint(a,b)
Return Value:
  • A return value is a value that returns to a procedure using a return statement
    • Python to continues executing the program and returns a certain value (Return Value)
    • Using a return statement

Quiz Score

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

Square Root

import math

def square_root():
    x = int(input("Input a number"))
    print(math.sqrt(x))

square_root()
2.0

Abstraction

This code needed abstraction so that I wouldn't have to write the code out multiple times, which to allowed it to be more concise.

place1 = int(input("Enter your score"))
place2 = int(input("Enter your score"))
combined = place1 + place2
def function(combined):
    if combined:
        print("Qualified for Regionals")
    else:
        print("Not qualified")

function(combined)
Qualified for Regionals
# 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





## MY HACK FOR 3.13 BELOW

def count_words_starting_with_x_letter(s):
    x = input("Input a letter")
    words = split_string(s)
    count = count_words_starting_with_letter(words, x)
    return count


s = "   Now you can choose which letter you want to test "
a_count = count_words_starting_with_a_in_string(s)
d_count = count_words_starting_with_d_in_string(s)
x_count = count_words_starting_with_x_letter(s)
print("Words starting with your letter:", x_count)



## ADD another function (ask for letter and then check which one)
# Define x = input
# count = count words starting with letter (words, x)
Words starting with your letter: 2

3.13 3.C HACKS

Procedure Name:

The name of a function

Arguments:

An argument provides data to a function

  • Often defined outside of a function
  • Imported into function via parameters

Buttons in Javascript

<!-- function is called here -->
<button id="enter" onclick="print(a,b)">ADD</button> 
<p id="result"></p>
<!-- javascript -->
<script>
    function print(a,b) {
        document.getElementById("result").innerHTML = a + b // math
    }
    // variables are defined
    var a = 5
    var b = 5

</script>

Vocabulary

  • Modularity - the practice of breaking a complex program into smaller, independent parts or modules that can be used and reused in different parts of the program
  • Abstraction - the practice of hiding the details of how a particular code or system works and exposing only the essential features or functions that are necessary for other parts of the program to use
  • Duplication - having multiple duplicate code blocks, often decreasing readability and efficiency
  • Logic - the sequence of steps and operations that a computer follows to execute a program, including the specific instructions and decision-making processes built into the code

3.13 Vocabulary

  • Procedure - a module of code that is created to complete a certain task, this is basically a function
  • Procedure Name - the name that is given to a function/procedure
  • Parameters - a variable that is used in a function to allow for data to be imported into a function
  • Arguments - a way to provide information to a function, usually defined outside a function and then imported into a function with parameters

3.13 Questions

function compare(a,b) {
    if(a>b) {
        console.log("a is greater than b")
    } else if (a<b) {
        console.log("a is less than b")
    }
}

// How do you call to this function?

what are the parameters?

  • a and b

What is the output?

  • "a is greater than b" or "a is less than b"

what are the arguments?

  • (a,>b), (a< b)