猿问

Python-比较两个字符串-无法循环

我正在尝试执行的操作:通过扫描每个字符串来确定两个字符串是否匹配(不使用比较运算符或cmp()内置函数)。


我的解决方案:


a = input('Please enter the first string to compare:')

b = input('Please enter the second string to compare: ')


while True:

    count = 0

    if a[count] != b[count]:             # code was intended to raise an alarm only after finding the first pair of different elements

        print ('Strings don\'t match! ')

        break

    else:                                # otherwise the loop goes on to scan the next pair of elements

        count = count + 1

问题: 经过测试,该脚本似乎只能比较[0]每个字符串中的第一个element()。如果两个字符串中的第一个元素相同(a[0] == b[0]),则不会继续扫描其余字符串。并且它在解释器中不返回任何内容。如果else执行套件,脚本也不会自行结束。



慕标5832272
浏览 247回答 3
3回答

杨__羊羊

您可以zip在这里使用:def compare(s1, s2):&nbsp; &nbsp;if len(s1) != len(s2):&nbsp; &nbsp; &nbsp; &nbsp;return False&nbsp; &nbsp;else:&nbsp; &nbsp; &nbsp; for c1, c2 in zip(s1, s2):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if c1 != c2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else:&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return True>>> compare('foo', 'foo')True>>> compare('foo', 'fof')False>>> compare('foo', 'fooo')False在您的代码中,您将在每次迭代中重置countto的值0:a = input('Please enter the first string to compare:')b = input('Please enter the second string to compare: ')if len(a) !=&nbsp; len(b):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # match the length first&nbsp; &nbsp; print ('Strings don\'t match')else:&nbsp; &nbsp; count = 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #declare it outside of while loop&nbsp; &nbsp; while count < len(a):&nbsp; &nbsp; &nbsp; &nbsp; #loop until count < length of strings&nbsp; &nbsp; &nbsp; &nbsp; if a[count] != b[count]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print ('Strings don\'t match! ')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; count = count + 1&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp;print ("string match")

偶然的你

您将count在每次循环迭代时将其重置为0。因此,循环会一遍又一遍地比较第一个元素。要解决该问题,只需将count = 0分配移到循环外即可,如下所示:# ...b = input('Please enter the second string to compare: ')count = 0while True:&nbsp; &nbsp; if a[count] != b[count]:# ...完成此操作后,您将意识到还有第二个问题–到达其中一个字符串的末尾时,程序将崩溃。您可能也想处理这种情况。

一只萌萌小番薯

a = raw_input('Please enter the first string to compare:')b = raw_input('Please enter the second string to compare: ')count = 0&nbsp;if len(a) == len(b):&nbsp; &nbsp; while count < len(a):&nbsp; &nbsp; &nbsp; &nbsp; if a[count] != b[count]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# code was intended to raise an alarm only after finding the first pair of different elements&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print ('Strings don\'t match! ')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # otherwise the loop goes on to scan the next pair of elements&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count = count + 1&nbsp;else:&nbsp; &nbsp; print "Strings are not of equal length"使用raw_input,将count放入while循环之外,并在True ==>时更改while count!= len(a),以防止“ IndexError:字符串索引超出范围”。
随时随地看视频慕课网APP

相关分类

Python
我要回答