我需要做一个程序来定义这个词是不是字谜。好吧,这是我的密码。标有## here
的行反映了主要问题。我需要定义第一个和第二个for
循环的word1
和word2
的索引,然后比较它们。如果这些索引是相似的,那么程序就会忽略这个词(因为它在迭代时出现两次)。我知道,代码还没有完成
text_sample = 'text goes here'
c = 0
i = 0
isanagramma = 0
n = len(text_sample)
while c < len(text_sample):
for word1 in text_sample:
for word2 in range(1,n):
s1 = sorted(word1)
s2 = sorted(text_sample[word2])
index1 = text_sample.index(word1) ## here
index2 = text_sample.index(text_sample[word2]) ## here
if index1 == index2: ## here
continue ## here
if s1 == s2:
isanagrama+=1
i+=1
c+=1
print(isanagramma)
输出:
相关问题 更多 >
编程相关推荐