Anatomy of Python (First Python post)
Notes on the anatomy of Python and answers to Hacks.
print("Hello World!")
msg = "Greetings"
print(msg)
def question_and_answer(prompt):
print("Question: " + prompt)
msg = input()
print("Answer: " + msg)
question_and_answer("Name the Python output command mentioned in this lesson?")
question_and_answer("If you see many lines of code in order, what would College Board call it?")
question_and_answer("Describe a keyword used in Python to define a function?")
from IPython.display import Image
from IPython.core.display import HTML
Image(url= "https://raw.githubusercontent.com/Azeem-Khan1/fastpages-project/master/images/quizpy%20snip.jpg")
Pseudocode
- PROCEDURE question_with_response (question)
- DISPLAY question
- response <-- input
- RETURN response
- questions <-- 5
- correct <-- 0
- DISPLAY
Hello {user}, welcome to the Python quiz!
- DISPLAY
You will be asked {# of questions} questions
- DISPLAY
All answers are to be in lower case only
- rsp <-- question_with_response(question)
- IF (rsp == answer)
- DISPLAY rsp is correct!
- correct <-- correct +1
- ELSE
- DISPLAY rsp is incorrect :(
- DISPLAY rsp is correct!
- rsp <-- question_with_response(question)
- IF (rsp == answer)
- DISPLAY rsp is correct!
- correct <-- correct +1
- ELSE
- DISPLAY rsp is incorrect :(
- DISPLAY rsp is correct!
- rsp <-- question_with_response(question)
- IF (rsp == answer)
- DISPLAY rsp is correct!
- correct <-- correct +1
- ELSE
- DISPLAY rsp is incorrect :(
- DISPLAY rsp is correct!
- rsp <-- question_with_response(question)
- IF (rsp == answer)
- DISPLAY rsp is correct!
- correct <-- correct +1
- ELSE
- DISPLAY rsp is incorrect :(
- DISPLAY rsp is correct!
- rsp <-- question_with_response(question)
- IF (rsp == answer)
- DISPLAY rsp is correct!
- correct <-- correct +1
- ELSE
- DISPLAY rsp is incorrect :(
- DISPLAY rsp is correct!
- percent <-- str(round(correct/questions), 1)
- DISPLAY
{user}, you scored {str(correct) / str(questions)}
- DISPLAY percent
- DISPLAY
Hope you enjoyed!
import getpass, sys # import necessities
def question_with_response(prompt): # function that allows for checking of correct response
print("Question: " + prompt)
msg = input()
return msg
questions = 5
correct = 0
print('Hello, ' + getpass.getuser() + ", welcome to the Python quiz!") # introduction
print("You will be asked " + str(questions) + " questions.")
print("All answers are to be in lower case only")
rsp = question_with_response("What is it called when a value or function is assigned to an easy-to-remember word?") # define the question that is being asked
if rsp == "variable": # if the user's answer matches the correct one, then...
print(rsp + " is correct!")
correct += 1 # add 1 point
else: # if not...
print(rsp + " is incorrect :(")
rsp = question_with_response("What command is used to include other functions that were previously developed?") # define the question that is being asked
if rsp == "import": # if the user's answer matches the correct one, then...
print(rsp + " is correct!")
correct += 1 # add 1 point
else: # if not...
print(rsp + " is incorrect :(")
rsp = question_with_response("Each 'if' command contains an '_________' to determine a true or false condition?") # define the question that is being asked
if rsp == "expression": # if the user's answer matches the correct one, then...
print(rsp + " is correct!")
correct += 1 # add 1 point
else: # if not...
print(rsp + " is incorrect :(")
rsp = question_with_response("What command is used to evaluate correct or incorrect response in this example?") # define the question that is being asked
if rsp == "if": # if the user's answer matches the correct one, then...
print(rsp + " is correct!")
correct += 1 # add 1 point
else: # if not...
print(rsp + " is incorrect :(")
rsp = question_with_response("What is grouping a sequence of commands, often used repeatedly, called?") # define the question that is being asked
if rsp == "procedural abstraction": # if the user's answer matches the correct one, then...
print(rsp + " is correct!")
correct += 1 # add 1 point
else: # if not...
print(rsp + " is incorrect :(")
percent = str(round(correct/questions, 1)*100) # calculate percentage by dividing # of correct answers by # of questions (round to the tenths place)
print(getpass.getuser() + ", you scored " + str(correct) +"/" + str(questions)) # show score (# of correct / # of questions)
print(f"Congratulations! That's a(n) {percent}%!") # show percentage
print("Hope you enjoyed!")