Python Quiz
Python quiz
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))
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!")
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)
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))
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!")