Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't convert 'list'object to str implicitly Python

I am trying to import the alphabet but split it so that each character is in one array but not one string. splitting it works but when I try to use it to find how many characters are in an inputted word I get the error 'TypeError: Can't convert 'list' object to str implicitly'. Does anyone know how I would go around solving this? Any help appreciated. The code is below.

import string
alphabet = string.ascii_letters
print (alphabet)
splitalphabet = list(alphabet)
print (splitalphabet)

x = 1
j = year3wordlist[x].find(splitalphabet)
k = year3studentwordlist[x].find(splitalphabet)
print (j)

EDIT: Sorry, my explanation is kinda bad, I was in a rush. What I am wanting to do is count each individual letter of a word because I am coding a spelling bee program. For example, if the correct word is 'because', and the user who is taking part in the spelling bee has entered 'becuase', I want the program to count the characters and location of the characters of the correct word AND the user's inputted word and compare them to give the student a mark - possibly by using some kind of point system. The problem I have is that I can't simply say if it is right or wrong, I have to award 1 mark if the word is close to being right, which is what I am trying to do. What I have tried to do in the code above is split the alphabet and then use this to try and find which characters have been used in the inputted word (the one in year3studentwordlist) versus the correct word (year3wordlist).

like image 839
user3112327 Avatar asked Sep 15 '26 06:09

user3112327


1 Answers

There is a much simpler solution if you use the in keyword. You don't even need to split the alphabet in order to check if a given character is in it:

year3wordlist = ['asdf123', 'dsfgsdfg435']
total_sum = 0
for word in year3wordlist:
    word_sum = 0
    for char in word:
        if char in string.ascii_letters:
            word_sum += 1
    total_sum += word_sum

# Length of characters in the ascii letters alphabet:
# total_sum == 12
# Length of all characters in all words:
# sum([len(w) for w in year3wordlist]) == 18

EDIT:

Since the OP comments he is trying to create a spelling bee contest, let me try to answer more specifically. The distance between a correctly spelled word and a similar string can be measured in many different ways. One of the most common ways is called 'edit distance' or 'Levenshtein distance'. This represents the number of insertions, deletions or substitutions that would be needed to rewrite the input string into the 'correct' one.

You can find that distance implemented in the Python-Levenshtein package. You can install it via pip:

$ sudo pip install python-Levenshtein

And then use it like this:

from __future__ import division
import Levenshtein

correct = 'because'
student = 'becuase'
distance = Levenshtein.distance(correct, student)  # distance == 2

mark = ( 1 - distance / len(correct)) * 10  # mark == 7.14

The last line is just a suggestion on how you could derive a grade from the distance between the student's input and the correct answer.

like image 50
logc Avatar answered Sep 16 '26 20:09

logc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!