【发布时间】:2020-09-21 01:00:10
【问题描述】:
给出的问题是:如何实现一个函数,以便在输入字符串长度相同且在一个字符位置不同时返回 True,否则返回 False。 我希望根据输入字符串的长度是否相同并且恰好在一个字符位置不同而返回真或假。我现在告诉我它们的长度是否相同,但在以不同顺序给出相同的一组字母时会失败。
def sameordiffernt(string1,string2):
str1 = string1.lower()
str2 = string2.lower()
count = 0
if len(str1) == len(str2):
print ('true1')
else:
print('false1')
count = count +1
while str1 == str2:
print('true2')
else:
print('false2')
count = count + 1
if count == 2:
print('false3')
else:
print('true')
return
【问题讨论】:
-
你能举个例子吗? “字符串长度相同,并且在一个字符位置上完全不同”
-
"car", "arc" 同一组字母但位置不同。
标签: python