Explanation of code

I imported the getpass and sys library. The quiz first recognizes the user's username and path to the Python interpreter.

The quiz then asks a few questions and answers, which are included in a list. Using the for loop, I can use recursion to iterate through each index in the list.

import getpass,sys

print("Welcome " + getpass.getuser() + ". Your Python interpreter is currently running on the path: " + sys.executable)

questions = ["What is the name for code that does not change every time you run the program?", "What is the Python command that allows user input?", "What do you use to combine different data types within the print statement?", "def foo(bar): What is bar also known as?", "What comparison operator do you use in Python to test if two values are equal to each other?", "What command is used to include other functions that were previously developed?", "What command is used to evaluate correct or incorrect response in this example?", "Each 'if' command contains an '_________' to determine a true or false condition?"]
answers = ["static text", "input", "concatenation", "parameter", "==", "import", "if", "expression"]

questionNum = len(questions)
score = 0

for i in range(questionNum):
    print(questions[i]) # This is solely so that the user can see the questions in the Jupyter Notebook output
    response = input(questions[i])
    print("Answer: " + response) # Again, to show the output
    if response == answers[i]:
        print("Correct!")
        score += 1
    else:
        print("Incorrect! The correct answer is: " + answers[i])
    
print("Your total score is: " + str(score) + " out of " + str(questionNum))
Welcome lwu1822. Your Python interpreter is currently running on the path: /home/lwu1822/anaconda3/bin/python
What is the name for code that does not change every time you run the program?
Answer: static text
Correct!
What is the Python command that allows user input?
Answer: input
Correct!
What do you use to combine different data types within the print statement?
Answer: ?
Incorrect! The correct answer is: concatenation
def foo(bar): What is bar also known as?
Answer: parameter
Correct!
What comparison operator do you use in Python to test if two values are equal to each other?
Answer: ==
Correct!
What command is used to include other functions that were previously developed?
Answer: import
Correct!
What command is used to evaluate correct or incorrect response in this example?
Answer: if
Correct!
Each 'if' command contains an '_________' to determine a true or false condition?
Answer: ?
Incorrect! The correct answer is: expression
Your total score is: 6 out of 8

How the rest of this post will be structured

This post captures my learnings from this page.

I will first document my learnings, and then display the code and its output below.


In programming languages, a lot of the things within parenthesis are called parameters.

For example, in the code below, Hello World! would be an argument.

Static text: Text that stays the same.

print("Hello World!")
Hello World!


Input and output

This is known as dynamic, versus static. Dynamic means that a variable can change each time you run the code. For example, each time you run the code, the input could change.

In Python code, the things specified in parenthesis are known as parameters. For example, in the code below, usr_input is a parameter.

usr_input = input("Enter some text: ")
print(usr_input)
hi :)


Function

Note: Indentation matters in Python

When creating a function, make sure to indent to tell Python which lines of code belong within the function (since Python does not use curly braces to delineate functions).

Functions are created with the def keyword.

def addNum(a, b):
    return int(a) + int(b)
     

num1 = input("Enter 1st number: ")
print("Enter 1st number: " + num1)
num2 = input("Enter 2nd number: ")
print("Enter 2nd number: " + num2) 

total = addNum(num1, num2)

print("Your total is: " + str(total))
Enter 1st number: 1
Enter 2nd number: 2
Your total is: 3


Libraries

Libraries can be imported with the - you guessed it - import function.

Examples of libraries include getpass and sys.

import getpass 
import sys 

print("You are: " + getpass.getuser())
print("Your Python interpreter is running at the file path: " + sys.executable)

passwd = getpass.getpass(prompt='Enter the password. Hint: what language does this program run in? ')
print("Enter the password. Hint: what language does this program run in? ")
print(passwd) 

if passwd == "python":
    print("Welcome! " + getpass.getuser())
else: 
    print("Incorrect!")
You are: lwu1822
Your Python interpreter is running at the file path: /home/lwu1822/anaconda3/bin/python
Enter the password. Hint: what language does this program run in? 
python
Welcome! lwu1822