Last active
March 3, 2021 03:11
-
-
Save PHenegan/4b7affaa93bef04bee3221ddcda46138 to your computer and use it in GitHub Desktop.
Python Quiz App for Mobile CSP Lesson 5.6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def main(): | |
answer = input("Would you like to take the quiz? (y/N): ") | |
if (answer.lower() == "yes" or answer.lower() == "y"): | |
print() | |
takeQuiz() | |
def takeQuiz(): | |
questionList, answerList = loadQuiz("quiz.txt") | |
userResponseList = [] | |
submitted = False | |
#returns if there is no quiz to take | |
if (len(questionList) == 0): | |
print("Error: Quiz file did not have any questions") | |
return | |
for i in range(len(questionList)): | |
userResponseList.append("") | |
#displays all of the questions and asks the user to pick one to answer | |
while (submitted == False): | |
response = "" | |
questionNum = 0 | |
print("\nEnter the number of the question you would like to answer, or type /submit to submit the quiz.\n") | |
for i in range(len(questionList)): | |
print(str(i + 1) + ".", questionList[i]) | |
response = input("\nQuestion #: ") | |
if (response.lower() == "/submit"): | |
print("Submitting...") | |
submitted = True | |
elif (response.isnumeric()): | |
questionNum = int(eval(response)) | |
userResponseList[questionNum - 1] = getResponse(questionList[questionNum - 1]) | |
else: | |
print("Invalid input entered. Please try again.") | |
score = getScore(userResponseList, answerList) | |
print("You got ", score, "% of the questions correct!") | |
printAnswers = False | |
print("Would you like to see the answers? (y/N): ") | |
#Loads the quiz from a specified text file and returns two lists containing the questions and answers. | |
def loadQuiz(fileName): | |
questions = [] | |
answers = [] | |
file = open(fileName, "r") | |
fileLines = file.readlines() | |
#Iterates through every line of the file and adds the ones with the proper indicators to the list | |
for x in fileLines: | |
if (len(x) <= 3): | |
continue | |
if (x[0:3] == "<p>"): | |
questions.append(x[3:len(x)-1]) | |
elif (x[0:3] == "<a>"): | |
answers.append(x[3:len(x)-1]) | |
return questions,answers | |
def getResponse(question): | |
print("\nQuestion:", question) | |
answer = input("Answer: ") | |
return answer | |
def getScore(responses, answers): | |
count = 0.0 | |
print(responses) | |
for i in range(len(answers)): | |
if (responses[i].lower() == answers[i].lower()): | |
count += 1 | |
percent = count / len(answers) * 100 | |
return percent | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment