inp = input("Enter word")
inplen = len(inp)
text = "sandwich"
textlen = len(text)
if inplen >= textlen:
if inp[0] == text[0]:
print("s")
if inplen >= textlen:
if inp[1] == text[1]:
print("a")
if inplen >= textlen:
if inp[2] == text[2]:
print("n")
if inplen >= textlen:
if inp[3] == text[3]:
print("d")
if inplen >= textlen:
if inp[4] == text[4]:
print("w")
if inplen >= textlen:
if inp[5] == text[5]:
print("i")
if inplen >= textlen:
if inp[6] == text[6]:
print("c")
if inplen >= textlen:
if inp[7] == text[7]:
print("h")
当我没有输入完整的“三明治”时,我不会得到输出。我想做的是,程序应该打印所有正确的字母,已经输入匹配“sandhich”。因此,当输入“sandwooh”时,程序应该返回“s”“a”“n”“d”“w”“h”,当输入“sand”时,程序应该返回“s”“a”“n”“d”。 谢谢
您只需按以下步骤进行:
set(a) & set(b)
返回一个集合,其中包含a
和b
共有的元素list()
然后将它们转换为一个列表,然后打印它们。举个例子:在这里,循环要容易得多:
相关问题 更多 >
编程相关推荐