Skip to content

Instantly share code, notes, and snippets.

@Mathsmaniac
Created August 24, 2021 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mathsmaniac/35e49af63e4fd31557c0d2c4c30e02df to your computer and use it in GitHub Desktop.
Save Mathsmaniac/35e49af63e4fd31557c0d2c4c30e02df to your computer and use it in GitHub Desktop.
Checks the users response against a model answer
"""Component 3 of elements Quiz project - Checking the users answer
Will check the users response against a model answer
Will also run a function that checks the spelling of the answer
Written by Nathan Smith
23/08/21"""
# This is the function from 3.5spell_check.py
def check_spell(correct_answer, user_answer):
checking_list = []
correct_list = []
errors = 0
# Putting all the words into lists so that I can manage them easier
for letter in correct_answer:
correct_list.append(letter)
for letter in user_answer:
checking_list.append(letter)
# If the length is not the right length or is not 1
# over the right length, the word must have more than 1
# error in it so it is automatically returning bad
# If the word is 1 letter over then that's fine because the spell
# checker lets through 1 mistake and an added letter is a mistake
if len(checking_list)-1 == len(correct_list) or\
len(checking_list) == len(correct_list):
pass
else:
return "bad"
# If the word is the right length then it does this bit of code
if len(checking_list) == len(correct_list):
# Goes through every letter of the word and
# checks it against the exemplar
for i in range(len(checking_list)):
if correct_list[i] == checking_list[i]:
pass
else:
try:
# This means that if the letters are mixed up it only
# gives 1 error in total, not 2
if (correct_list[i] == checking_list[i-1] and
correct_list[i-1] == checking_list[i]) or \
(correct_list[i] == checking_list[i+1] and
correct_list[i+1] == checking_list[i]):
errors += 0.5
else:
errors += 1
except IndexError:
errors += 1
# This bit sees if the extra letter is the only mistake in the word
else:
for i in range(len(checking_list)-1):
if correct_list[i] == checking_list[i]:
pass
else:
if len(checking_list) == len(correct_list)+1:
del checking_list[i]
errors += 1
if errors > 1:
return "bad"
else:
return "fine"
def ask_question(question, answer, question_type):
global right_answers
global wrong_answers
# Makes it so that it only spell checks if it is asking for the element
if question_type == "element":
tries = 0
# Gets the input
user_answer = input(question)
while True:
# Makes it so that it is not case sensitive
if user_answer.lower() == answer.lower():
print("Correct!")
right_answers += 1
break
else:
# If it is wrong, run it through the spell checking function
foo = check_spell(answer, user_answer)
if foo == "bad":
print("Sorry, that was not the correct answer, "
"\nThe correct answer was: {}\n".format(answer))
wrong_answers += 1
break
else:
# Makes it so that once they have spelt it wrong once
# they can only have one more try to get it right
if tries < MAX_TRIES:
print("Are you sure you've spelt that"
" correctly?\nHave another go: \n")
user_answer = input(question)
tries += 1
continue
else:
print("Sorry, that was not the correct answer, "
"\nThe correct answer was: {}\n".format(answer))
wrong_answers += 1
break
else:
# The part for if it is asking about a symbol
user_answer = input(question)
if user_answer == answer:
print("Correct!")
right_answers += 1
# Makes it so that if the error is in the case
# then it tells the user that
elif user_answer.lower() == answer.lower():
print("Sorry, that's incorrect, remember"
" that the symbols are case sensitive")
wrong_answers += 1
else:
print("Sorry, that was not the correct answer, "
"\nThe correct answer was: {}\n".format(answer))
wrong_answers += 1
MAX_TRIES = 1
right_answers = 0
wrong_answers = 0
ask_question("", "Na", "symbol")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment