Python Basics(Week 1)
Page to show basic python skills learned in Week 1.
print ("Hello world") # This is a comment. Allows one to explain their work.
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))
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.
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.