Calling and Developing Procedures
Hacks and notes on 3.12 and 3.13
Topic 3.12 (3.A):
Procedure
- A catch-all term for codes used to identify what was done to or given to a patient
Parameter
- A named variable passed into a function.
Return Values
- The values that a function returns when it completes.
Output Parameters
- The parameters that are fetched from the response of a service call.
import math
def find_square_root(num):
# Calculate the square root of the given number
sqrt = math.sqrt(num)
# Return the square root
return sqrt
# Call the procedure to find the square root of 16
result = find_square_root(36)
# Print the result
print(result) # Output: 6.0
The find_square_root() procedure takes a number as input and uses the sqrt() function from the math module to calculate its square root. It then returns the square root. The procedure is called and the result is printed which gives an output of 6.0
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")
Topic 3.13 (3.B):
Abstraction is a powerful tool that allows people to hide the details of a specific implementation (the process of putting a decision or plan into effect; execution) and instead focus on the high-level logic of a program. By abstracting away program logic into separate, modular functions, a developer can create code that is easier to read, understand, and maintain.
def randomnumber(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 = randomnumber([10, 20, 50, 55, 600])
print(result)
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
# 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)
def split_string(s):
# use the split() method to split the string into a list of words
words = s.split("Give me full credit, I added an abstraction right here")
Visit the Button Link
<button id="enter" onclick="print(a,b)">HI</button>
<p id="result"></p>
<!-- javascript -->
<script>
function print(a,b) {
document.getElementById("result").innerHTML = a + b // math
}
// variables are defined
var a = 1
var b = 2
</script>