Lesson 3.12 & 3.13 Hacks
Hacks
Topic 3.12 (3.A):
- Define procedure and parameter in your own words
- procedure: a smaller section of code that is referenced another bigger code.
- parameter: a kind of variable that uses a sub routine
- Paste a screenshot of completion of the quiz
- Define Return Values and Output Parameters in your own words
- Return Values: data that is outputted by a function after it has been executed
- Output Parameters: the values that are returned by a function after it has been executed.
- Code a procedure that finds the square root of any given number. (make sure to call and return the function)
from math import sqrt
def find_sqrt(num):
result = sqrt(num)
return result
input = int(input())
result = find_sqrt(input)
print("sqrt(", input, ") = ", result)
Topic 3.13 (3.B):
-
Explain, in your own words, why abstracting away your program logic into separate, modular functions is effective
- Abstracting away your program logic into separate, modular functions is effective because it allows you to write code that is organized, modular, and reusable.
-
Create a procedure that uses other sub-procedures (other functions) within it and explain why the abstraction was needed (conciseness, shared behavior, etc.)
def calc_average(numbers):
def find_sum(nums):
total = 0
for num in nums:
total += num
return total
def find_length(nums):
length = 0
for num in nums:
length += 1
return length
sum_of_numbers = find_sum(numbers)
length_of_numbers = find_length(numbers)
average = sum_of_numbers / length_of_numbers
return average
result = calc_average([1, 2, 3, 4, 5])
print(result)
- 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?)
def calc_average(numbers):
def find_sum(nums):
total = 0
for num in nums:
total += num
return total
def find_length(nums):
length = 0
for num in nums:
length += 1
return length
def print_num(numbers):
print(calc_average)
sum_of_numbers = find_sum(numbers)
length_of_numbers = find_length(numbers)
average = sum_of_numbers / length_of_numbers
return average
result = calc_average([1, 2, 3, 4, 5])
print(result)
Topic 3.13 (3.C):
- Define procedure names and arguments in your own words.
- used to identify the specific procedure that needs to be executed
- 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
<html>
<head>
<title>Calculator</title>
</head>
<body>
<h1>Calculator</h1>
<p>Enter two numbers and an operator to perform a calculation.</p>
<input id="num1" type="number" placeholder="Enter a number">
<input id="num2" type="number" placeholder="Enter another number">
<select id="operator">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<button id="calculate">Calculate</button>
<p id="result"></p>
<script>
const num1 = document.getElementById('num1');
const num2 = document.getElementById('num2');
const operator = document.getElementById('operator');
document.getElementById('calculate').addEventListener('click', function() {
const n1 = num1.value;
const n2 = num2.value;
const op = operator.value;
let result = 0;
if (op === '+') {
result = n1 + n2;
} else if (op === '-') {
result = n1 - n2;
} else if (op === '*') {
result = n1 * n2;
} else if (op === '/') {
result = n1 / n2;
}
document.getElementById('result').innerHTML = result;
});
</script>
</body>
</html>