Skip to content

Instantly share code, notes, and snippets.

@Mathsmaniac
Created August 24, 2021 01:44
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/46fd4841293e18ad77a3fb84b149b0b0 to your computer and use it in GitHub Desktop.
Save Mathsmaniac/46fd4841293e18ad77a3fb84b149b0b0 to your computer and use it in GitHub Desktop.
This program checks a response agianst a model response and sees whether there were accidental mistakes in it
"""Component 3.5 of elements Quiz project - Spell checker
This program will check a response against a model response
It will let slip ONE of the following: two letters mixed up (e.g. Soduim)
A letter out of place (e.g. Sodiun), or an added letter (e.g. Soedium)
It will either return "bad" which means the answer should be marked wrong
or "fine" which means that the user should be asked to check their spelling
Written by Nathan Smith
23/08/2021"""
# Making a function to spell check with two parameters
def check_spell(correct_answer, user_answer):
correct_list = []
checking_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"
correct = "sodium"
asking = input("What would you like to spell check? ")
print(check_spell("sodium", asking))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment