Use a for loop please! Create a Python function called countDiffs that takes two
ID: 3858963 • Letter: U
Question
Use a for loop please!
Create a Python function called countDiffs that takes two arguments, both of which are strings of the same length. (You don't need to verify that the strings are the same length.) Your function should compare the two strings, character by character, and counts the number of times the two strings have different characters in the same location (i.e., at the same integer index). Your function should then return that number. Here are some examples of how your function should behave:
>>> countDiffs("abcdef","acceef")
2
>>> countDiffs("abc","abc")
0
>>> countDiffs("abc","xyz")
3
>>> countDiffs("","")
0
Explanation / Answer
def countDiffs(s1,s2):
count = 0
for i in range(0, len(s1)):
if s1[i] != s2[i]:
count+=1
return count
print(countDiffs("abcdef","acceef"))
print(countDiffs("abc","abc"))
print(countDiffs("abc","xyz"))
print(countDiffs("",""))
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.