我想比较n个单词。单词的第一个和最后一个字母必须相同,下一个单词必须从结尾开始,并以字母表中的下一个字母结束。如果所有单词都正确,则打印'ok'。
例如:3利物浦MaM纳尔逊
'OK'
n = int(input("Input a number: "))
woorden = []
for i in range(n):
woord = input("Enter a string: ")
woorden.append(woord)
if woord[:1].startswith('"') == woord[:-1].endswith('"'):
if ord(woord[:-1]) == ord(woord[:1]):
print('ok')
else:
print(f'{woord}')
答案 0 :(得分:0)
你的if \ else缩进是坏的。 只有在检查完循环后的整个项目AKA后才需要进行打印。
n = int(input("Input a number: "))
allGood = True
woorden = []
for i in range(n):
woord = input("Enter a string: ")
woorden.append(woord)
if woord[:1].startswith('"') == woord[:-1].endswith('"'):
if ord(woord[:-1]) != ord(woord[:1]):
allGood = False
print allGood
答案 1 :(得分:0)
可能是这样的:
>>> n = int(input("Input a number: "))
Input a number: 3
>>> woorden = []
>>> for i in range(n):
word = raw_input('Enter a string: ')
woorden.append(word)
if word[0] != word[-1]:
break
if i==0:
previous_char = ord(word[0].lower())
continue
if ord(word[0].lower()) != (previous_char+1):
break
previous_char = ord(word[0].lower())
else:
print 'Ok'
Enter a string: liverpool
Enter a string: MaM
Enter a string: nelson
Ok
答案 2 :(得分:0)
假设这是预期的输出
Input a number: 5
Enter a string: This
Enter a string: test
Enter a string: is
is
Enter a string: true
Enter a string: fine
ok
代码
n = int(input("Input a number: "))
x = []
ok = False
for i in range(0,n):
woord = input("Enter a string: ")
if i > 0:
y = ord(woord[:1].lower()) -96
z = ord(woord[-1].lower())-96
x.append(y)
x.append(z)
if x[c-1] - y == -1:
ok = True
else:
print(f'{woord}')
ok = False
c=c+2
else:
x.append(ord(woord[-1].lower())-96)
c=1
if (ok):
print('ok')
对于提及的情景
Input a number: 3
Enter a string: liverpool
Enter a string: MaM
Enter a string: nelson
ok
答案 3 :(得分:0)
这段代码似乎适用于所有情况但不包含以下字样:' Zhanaz'其次是艾丽西亚'。 谁知道如何解决这个问题?
n = int(input("Input a number: "))
woorden = []
for i in range(n):
woord = input("Enter a string: ")
woorden.append(woord)
if woord[0].lower() != woord[-1].lower():
print(f'{woord}')
break
if i == 0:
previous_char = ord(woord[0].lower())
continue
if ord(woord[0].lower()) != (previous_char+1):
print(f'{woord}')
break
previous_char = ord(woord[0].lower())
else:
print('Ok')