3.12 and 3.13 Hacks
These are my 3.12 and 3.13 Hacks.
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")
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)
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")
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.
a = 2
b = 5