Python hacks

Usage of loops

Referenced code:

import getpass

# question format, gets user answer
def question_with_response(prompt):
    print("Question: " + prompt)
    msg = input()
    return msg

# lists of questions/answers
prompts=["What command would you use to display text into the terminal?", "What command would you use to ask the user for information?", "What is the keyword used in Python to define a function?", "How would you write a print statement to concatenate a string containing variable A with string 'Hello'? (include print() and use '' instead of \"\"", "How do we add a pre-made library to our code?", "We use _______ to store information for later use.", "Given the code, {variable = 1} what type of variable is this?", "Given the code, {variable = 1.1} what type of variable is this?", "Given the code, {variable = 'Hello'} what type of variable is this?"]
answers=["print", "input", "def", "print('hello' + a)", "import", "variables", "integer", "float", "string"]

correct = 0
questions = len(prompts)

# corrects answers, awards a point if correct
def gradeResponse(response, answer):
    global correct
    # convert to lowercase so ppl don't yell at me for scoring them as wrong when they went "prinT"
    if response.lower() == answer:
        print(f"{response} is correct!\nGood job :D")
        correct += 1
    else:
        print(f"{response} is incorrect!\nBetter luck next time :(")

# ask user if they want to proceed, exit if no
print(f"Hello {getpass.getuser()}!")
print(f"You will be asked {str(questions)} questions.")
rsp = question_with_response("Are you ready to take a quiz? [Y/n]")
if rsp.lower() == "y":
    print("Lets get started!")
else:
    print("Bye :(")
    exit()

# cycle through all questions, score the questions
for i in range(0, questions):
    rsp = question_with_response(prompts[i])
    gradeResponse(rsp, answers[i])

# calculate percentage
percentage=(correct/questions)*100
percentage=round(percentage, 2)

# report score
print(f"You got a total of {correct} out of {questions} ({percentage}%)!")

For loop

As seen here:

for i in range(0, questions):
    rsp = question_with_response(prompts[i])
    gradeResponse(rsp, answers[i])

The for loop is looping through all questions and grading the responses. It is doing this by referencing the index of the value in the list of all questions. I am able to make sure that no matter the size of the list, I am always able to be confident that I will loop through all questions, as I am using the the variable questions, representing the total amount of entries in the list.

If statement

As seen here:

def gradeResponse(response, answer):
    global correct
    # convert to lowercase so ppl don't yell at me for scoring them as wrong when they went "prinT"
    if response.lower() == answer:
        print(f"{response} is correct!\nGood job :D")
        correct += 1
    else:
        print(f"{response} is incorrect!\nBetter luck next time :(")

The if statement is checking if the answers response corresponds to the answer in the answers list. If so, then it will award the user with a point, as well as letting them know that they were correct. If the user was wrong, meaning their response didn't match the defined answer, the program will not add a point, and let the user know that they have gotten the question incorrect.

While loop

As there is no while loop in this program, I will provide an example of a possible usage of a while loop here:

i = 0
while i < 10:
    i += 1
    print(f"Hello World {i}!")

This while statement will print out the statement "Hello world {i}", where i is the current value that the loop is on. This loop will print out a total of 10 statements, from the values of 1, all the way up to 10.