Click here to Skip to main content
15,311,014 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Python
t=int(input())
for i in range(int(t)):
    str1=input().lower()
    str2=input().lower()
    n=False
    for i in range(len(str1)):
        if (str1[i]!=str2[i]) and (str1[i]!="?" and str2[i]!="?"):
                n=True
                break
    if n:
        print("NO")        
    else:
        print("YES")  


What I have tried:

Posted
Updated 14-Mar-21 21:29pm
Comments
Patrice T 15-Mar-21 2:57am
   
What error ?

1 solution

If you mean the error message is
IndexError: string index out of range
Then it's pretty simple, but it depends on your inputs.
if you enter "abcd" for your first string, and "?" as your second (or just "a") then the second time round the inner loop it tries to access the second character of the second string, which doesn't exist and you get an error.

Add a check before the loop which makes sure that both strings are the same length and it'll go away.

BTW: you could have found this very easily for yourself by using the debugger to see exactly what is happening when your code runs ... get used to the debugger, it can save you loads and loads of hair pulling wasted time!
   

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