Hack 1

Procedure: named set of instructions that can take in parameters and return values Paremeter: independent variables used in procedure to achieve result Procedures can be classified as sequence, selection, and iteration

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

Return values: exits function and directs python to continue to execute the program to return a certain value

Output Parameters: parameters passed to get an output of a function/procedure

import math

number = input("Input a number you would like to take the square root of")

def square_root(num):
    result = math.sqrt(num)
    return result

square_root_input = square_root(int(number)) # calculates the square root of the input (ex. 36)
squareone = square_root(9) # calculates the square root of 9
squaretwo = square_root(16) # calculated the square root of 16

print('The square root is: ', squareone)
print('The square root is: ', squaretwo)
print('The square root of ', number, " is ", square_root_input)
The square root is:  3.0
The square root is:  4.0
The square root of  9  is  3.0

Hack 2

Abstracting your program into separate, modular functions is a way of simplifying your code so that it can be reused throughout the program without having to be rewritten.

questionnumber = 3
meals = []
questions = [
    "What did you eat for breakfast? Pancakes, eggs, or cereal?",
    "What did you eat for lunch? A sandwich, mac and cheese, or Pizza?",
    "What did you eat for dinner? Paluk Paneer, pasta, or a salad?"
]

def mealask(question):
    print("Question: ", question)
    response = input()
    print("Your response: ", response)
    meals.append(response)

for number in range(questionnumber):
    mealask(questions[number])

for i in meals:
    if i == "cereal" or i == "lunchable" or i == "pizza":
        print(i, " was not a healthy choice")
    elif i == "bacon" or i == "mac and cheese" or i == "pasta":
        print(i, " was not a bad choice")
    else:
        print(i, " was a healthy choice")
Question:  What did you eat for breakfast? Pancakes, eggs, or cereal?
Your response:  Pancakes
Question:  What did you eat for lunch? A sandwich, mac and cheese, or Pizza?
Your response:  mac and cheese
Question:  What did you eat for dinner? Paluk Paneer, pasta, or a salad?
Your response:  pasta
Pancakes  was a healthy choice
mac and cheese  was not a bad choice
pasta  was not a bad choice

In the algorithim above, abstraction was needed due to shared behaior and conciseness. Without abstraction, I would have had to write all of the questions, their inputs, and the responses out individually. This saved me time and made the code more simple to follow and look at because I did not have to write everything down in the code.

Hack 3

Procedure names are names that describe a function and can be used to call a function. Procedure names make it easier for other programers to identify the purpose of a function and use throughout a program. Arguments are within parameters which provide functions with information.

a = 2

b = 5