Basic Function

Printing the famous "Hello world" statement.

print ("Hello world") # This is a comment. Allows one to explain their work.
Hello world

Running quiz.py

Using the given code from the APCSP website and running the code.

import getpass, sys

def question_and_answer(prompt):
    print("Question: " + prompt)
    msg = input()
    print("Answer: " + msg)

def question_with_response(prompt):
    print("Question: " + prompt)
    msg = input()
    return msg

questions = 3
correct = 0

print('Hello, ' + getpass.getuser() + " running " + sys.executable)
print("You will be asked " + str(questions) + " questions.")
question_and_answer("Are you ready to take a test?")

rsp = question_with_response("What command is used to include other functions that were previously developed?")
if rsp == "import":
    print(rsp + " is correct!")
    correct += 1
else:
    print(rsp + " is incorrect!")

rsp = question_with_response("What command is used to evaluate correct or incorrect response in this example?")
if rsp == "if":
    print(rsp + " is correct!")
    correct += 1
else:
    print(rsp + " is incorrect!")

rsp = question_with_response("Each 'if' command contains an '_________' to determine a true or false condition?")
if rsp == "expression":
    print(rsp + " is correct!")
    correct += 1
else:
    print(rsp + " is incorrect!")

print(getpass.getuser() + " you scored " + str(correct) +"/" + str(questions))
Hello, poonam running /opt/anaconda3/bin/python
You will be asked 3 questions.
Question: Are you ready to take a test?
Answer: Yes!
Question: What command is used to include other functions that were previously developed?
import is correct!
Question: What command is used to evaluate correct or incorrect response in this example?
if is correct!
Question: Each 'if' command contains an '_________' to determine a true or false condition?
expression is correct!
poonam you scored 3/3

My Quiz

The quiz which I have created, based on the functions learned in the lesson. Takes advantage of str() to convert floats(numbers) to strings. The use of .lower() also helps to make sure any capitalization configuration of the answer will be marked as correct.

import getpass, sys, math # Getting necessary libraries to run the code.

# Function with question and response structure.
def question(prompt): 
    print("Question: " + prompt) # Prints question in clear way.
    respond = input() # Gets an answer for the question from the user.
    print("Answer: " + respond)
    return respond # Important to return.

# Creating a list for the questions
list_question = ["What do you call multiple lines of code?", 
"How do you get a message from the user?", 
"Are numbers a string? Yes or No", 
"What does a function take in?", 
"If the if portion of a code is not met, what runs?"]

# Creating a list for the answers
answer_key = ["sequence", "input", "no", "parameter", "else"]

# Variables
question_count = len(list_question) # In this case, question_count = 5, which is the number of indexes in the list
score = 0 # Running score on quiz.

# Greeting
print('Hello, ' + getpass.getuser() + ".") 
question("How has your day been?")
print("I hope this quiz brightens your day.")
print("You will answer " + str(question_count) + " quiz questions.")

# For loop to repeat
for i in range(len(list_question)): # for each i in the range of the question list
    response = question(list_question[i]) # using the defined function
    check = answer_key[i] # variable for the answer
    if response.lower() == check: # .lower() helps account for capitalization
        print("Correct!")
        score += 1 # Add one to score
    else:
        print("Incorrect.")



# Function for finding percent.
def grade(sco, quests): 
    percent = 100 * float(sco)/float(quests)
    return percent

quiz_percentage = grade(score, question_count) # Defining variable

print("You got " + str(score) + " of " + str(question_count) + " questions correct.")
print("Your score is " + str(quiz_percentage) + "%.") # Final print statement with score, as a percentage.
Hello, poonam.
Question: How has your day been?
Answer: Great!
I hope this quiz brightens your day.
You will answer 5 quiz questions.
Question: What do you call multiple lines of code?
Answer: SEQUEnce
Correct!
Question: How do you get a message from the user?
Answer: ask
Incorrect.
Question: Are numbers a string? Yes or No
Answer: NO
Correct!
Question: What does a function take in?
Answer: parameter
Correct!
Question: If the if portion of a code is not met, what runs?
Answer: else
Correct!
You got 4 of 5 questions correct.
Your score is 80.0%.

Running the quiz, we can see it can correctly see if the user types in the right answer or not. Additionally, the code correctly grades the quiz, giving both the number of questions correct and the percentage. Using the for command helps to reduce the amount of code in the program, and the use of lists helps to make this code more adaptable to changes in the question and answer list.