我正在尝试制作一个程序来检测输入单词的每个字母是否按顺序排列在每个单词中。 我的程序就是如此。
Word: book
Sentence: look wow lap kill
no
yes
no
yes
输入一个单词时效果很好。当我输入两个或更多单词时,我试图让它工作。我想要的输出就是这个。
Word: book dogs cowl
Sentence: look wow lap kill
book:
no
yes
no
yes
dogs:
no
yes
no
no
cowl:
no
yes
no
yes
我的代码仅适用于输入的一个单词。我明白如果我改变名为cc的变量,它将改变与句子进行比较的单词。例如,如果cc被改为1,被比较的单词将成为狗而不是书。这允许我一次只比较一个单词,但我想同时比较所有估算的单词。我不确定如何将它实现到循环中。
f = input("Word: ")
gg = f.split(" ")
m = input("Sentence: ")
n = m.split(" ")
y = []
c = 0
cc = 0 #CHANGE THIS AND IT CHANGES THE WORD THAT IS BEING COMPARED TO THE SENTENCE
g = (gg[cc])
l = list(g[c])
while c < len(g):
if g[c] in n[c]:
print("yes")
if g not in y:
y.append(g)
else:
y[:] = [item for item in y if item != g]
print("no")
c = c+1
答案 0 :(得分:1)
你需要这样做,
f = input("Word: ")
gg = f.split(" ")
m = input("Sentence: ")
n = m.split(" ")
for i in gg:
print('%s:'%i)
for ind,j in enumerate(i):
if j in n[ind]:
print('%s- Yes'%j)
else:
print('%s- No'%j)
这是输出,
mohideen@botvfx-dev:~$ python Desktop/run.py
Word: book dogs cowl
Sentence: look wow lap kill
book:
b - No
o - Yes
o - No
k - Yes
dogs:
d - No
o - Yes
g - No
s - No
cowl:
c - No
o - Yes
w - No
l - Yes
mohideen@botvfx-dev:~$
答案 1 :(得分:1)
试试这个...这应该对你有用
word = input("Words: ")
words = word.split(" ")
sentence = input("Sentence: ")
sentence = sentence.split(" ")
for a_word in words:
j=0
for letter in a_word:
if letter in sentence[j]:
print("Yes")
else:
print("No")
j+=1