列出其他梯形图

2024-06-01 07:51:51 发布

您现在位置:Python中文网/ 问答频道 /正文

给定一个句子,返回该句子将其字母在字母表中转置为1,但仅当字母为a-y时

# input
'the quick brown fox jumps over the lazy dog'
# output
'uif rvjdl cspxo gpy kvnqt pwfs uif mbzy eph'

我试过这个:

a = [chr(ord(i) + 1) if ord(i) != ord(' ') else i if i =='y' else i for i in input() if ord(i) < ord('z')]
print(''.join(a))

我得到的输出(y在lazy word中缺少,即mbzy):

uif rvjdl cspxo gpy kvnqt pwfs uif mbz eph 

Tags: theinputif字母lazy句子ordeph
3条回答

这项工作:

a = 'the quick brown fox jumps over the lazy dog'
print(''.join([c if c == ' ' or ord(c) >= ord('y') else chr(ord(c) + 1) for c in a]))

输出

uif rvjdl cspxo gpy kvnqt pwfs uif mbzy eph

下面的代码应该可以做到这一点。我更改了一些条件,并删除了结尾(ord(i) < ord('z'))处的条件,因为它阻止处理“z”字符

此外,比较运算符处理字符和整数,因此在条件中不必总是使用“ord()”

a = [chr(ord(i) + 1) if i != ' ' and i < 'y' else i for i in input()]

print(''.join(a))

如果我理解了说明,则此代码对我有效:

what_to_return = ""
for char in input():
    what_to_return += char if char == 'y' or char == 'z' else chr(ord(char) + 1) if 'a' <= char < 'z' else " "

输出:

uif rvjdl cspxo gpy kvnqt pwfs uif mbzy eph

相关问题 更多 >