3.12 and 3.13 Hacks
Topic 3.12 (3.A):
- Define procedure and parameter in your own words
- Procedure: Also called a function or method, is something that has a given set of instructions that takes in a variable and returns a value.
- Parameters: They are like variables that are put into the procedure to produce the result. It is independent because it does not have a definite value.
- Paste a screenshot of completion of the quiz
- Quiz code shown below)
- Define Return Values and Output Parameters in your own words
- Return Values: a value that a function returns to the calling function when it completes its task or given instructions.
- Output Parameter: the parameters that are called from the function. Kind of like the thing that is assigned for the parameter, which is originally an independent variable that can hold any value.
- Code a procedure that finds the square root of any given number. (make sure to call and return the 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")
import math
A = int(input("Input a Number"))
def root(A):
return math.sqrt(A)
root(A)
Topic 3.13 (3.B):
- Explain, in your own words, why abstracting away your program logic into separate, modular functions is effective
- Abstracting the program logic into separate, modular functions is effective because it can make independent parts that can be used in other parts of the code, which is more efficient than just reprinting the code over and over again (it's also more time efficient).
- Create a procedure that uses other sub-procedures (other functions) within it and explain why the abstraction was needed (conciseness, shared behavior, etc.)
- 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?)
print("welcome to a shop")
print("shirts are 5, pants are 10")
shirt = 5
pants = 10
def shirtply(s):
return s * 5
def pantsply(s):
return s * 10
def tax(s):
return s * 0.08
print("How many shirts did you buy?")
x = int(input())
print(x)
print("That alone will be", shirtply(x))
print("How many pants did you buy?")
y = int(input())
print("That will be", pantsply(y))
print("All together, that will be", (shirtply(x) + pantsply(y)))
print("Your tax will be", tax((shirtply(x) + pantsply(y))))
print("Have a good day!")
Explanation
I created two functions that basically totalled the value for a particular amount of clothing that the person bought. Because of this, it shortened the amount of time that it took to code as I did not have to create new variables to find the value of how much the shirts, pants, and tax instead of simply typing something as simply as "tax()" Below is what the code would be without the defined sub procedures
print("welcome to a shop")
print("shirts are 5, pants are 10")
shirt = 5
pants = 10
print("How many shirts did you buy?")
x = int(input())
print(x)
prod = x * 5
print("Your total for those items alone with be",prod)
print("How many pants did you buy?")
y = int(input())
prod2 = y * 10
print("That will be", prod2)
print("All together, they will be", (prod + prod2))
print("Your tax will be", ((prod + prod2)*0.08))
print("Have a good day")
There are a lot more variables that are scattered throughout the code, which can make it way too confusing for someone in the future. Just having something as simple as shirtply() is a lot easier to read. Less errors will occur and it is more organized.
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
def letter_that_this_word_starts_with(s):
words = split_string(s)
count = letter_that_this_word_starts_with(words, s[1])
# example usage:
s = " This is a test string! Don't you think this is cool? "
a_count = count_words_starting_with_a_in_string(s)
d_count = count_words_starting_with_d_in_string(s)
print("Words starting with a:", a_count)
print("Words starting with d:", d_count)
Topic 3.13 (3.C)
Define procedure names and arguments in your own words.
- Procedure Names: Procedure names are just names that are given to the procedure()
Arguments: The values that take the place of the independent variables (the parameters) in the procedure when using the function somewhere else in the code
# Example def add(a,b) answer = a + b return answer add(2,3) # These are the arguments
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
Talked to Teacher, said it was okay for me to us MD to show I understand
let a == 10
let b == 5
function add(a,b) {
return a + b;
}
function sub(a,b) {
return a - b
}
function mult(a,b) {
return a * b
}
function div(a,b) {
return a / b
}
var button = document.createElement("button");
button.innerHTML = "Do Something";
var body = document.getElementsByTagName("body")[0];
body.appendChild(button);
button.addEventListener ("click", function() {
alert(add(a,b));
});
function add(a,b)
const btn = document.createElement('Button');
undefined
btn.innerText = 'Add A + B';
"Add A + B"
document.body.appendChild(btn);
<button>Add A and B</button>