Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There is dictionary in python.key and value spelling is compared.if the mistake is greater than or equal to 2 than print incorrect

input={"their":"thuyr"}

output=incorrect(because t=t,h=h but e!=u,i!=y).

My problem is that i was unable to compare t==t,h==h,e==u,i==y. The below code shows count value 22 but count value must be 2 because only two words mismatches with their

What I have tried:

def find_correct(words_dict):
count=0
for key,value in words_dict.items():
for val in value:
for ky in key:
if(val!=ky):
count+=1
return count

print(find_correct({"their":"thuor"}))
Posted
Updated 25-Sep-18 6:17am

1 solution

The problem is that you have two loops, so for each character in the value you are comparing against each character in the key. That makes 25 actual tests, and 22 of them will be not equal. Chang to the following:
Python
def find_correct(words_dict):
    count=0
    for key,value in words_dict.items():
        for i in range(len(value)): # this may need adjusting for different length words
            if(value[i]!=key[i]):
                count+=1 
    return count 

print(find_correct({"their":"thuor"}))
 
Share this answer
 
Comments
Tanya Chaudhary 25-Sep-18 15:55pm    
thanks @richard!! I have been trying this code for so long.Thanks once again for helping me out.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900