Comparing 2 strings


#1

Hi all,
I would like to write a code using python to differentiate difference between 2 strings
There should be 3 results. Good , Almost good , Bad

i) Good which means second string contains in the first string. Eg: smack and mac

ii) Almost good which means part of second string contained in first string. Eg: Rice and Race. both differ by 1 character.

iii)Bad which means more than 1 character is different compared when 2 strings are compared.

Be aware that:
i) position are also important.
tops and post have same characters, but only in 1st position(o) they are the same. hence, more than 1 difference between 2 strings.
ii) different in length between 2 strings.
duplicate and* cake* should return almost good , expression and press should return good
iii) code should automatically compare strings in lower case.

iv) importing is not allowed

Here’s my algorithm, but i failed to implement a rule at which only 1 character is allowed to be different in order to be categorized as almost good

Sa=input('enter something A: ')
Sb=input('enter something B: ')
Sc=Sa.lower()
Sd=Sb.lower()

count = 0
for val in Sc:
        for char in Sd:
            if char == val:
                count = count + 1
            
if count == 0:
    print("Bad")
elif Sc == Sd:
    print('Good')
else:
    print('Almost Good')


#2

for scenario 1, truth be told i would simply do:

if Sd in Sc:
    print("Good")

i also advice you to follow pythons naming convention for naming variables:


#3

I will pay attention to this! Thank you :slight_smile:


#4

so for example you could name the variable word_one and word_two, which are pretty logic variable names for what they do.

also, i would just add the lower to the input function:

word_one = input('enter something A: ').lower()
word_two = input('enter something B: ').lower()

you are going to convert to lowercase anyway, might as well do it directly

don’t you need to verify the user input? Or not part of the challenge?


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.