我想写一个简单的程序来交叉加密一个单词。 (即改变" A"到" Z"," B"到" Y"等等)我是python的新手并且无法获得循环工作。 这就是我所拥有的。我意识到可能有更好的方法来做到这一点。但我试图用最基本的语言基础知识来做到这一点。
w = str.upper(input("Enter a word\n"))
l = list(w)
for i in l:
if l[i] is "A":
l[i] = "Z"
elif l[i] is "B":
l[i] = "y"
elif l[i] is "C":
l[i] = "X"
elif l[i] is "D":
l[i] = "W"
elif l[i] is "E":
l[i] = "V"
elif l[i] is "F":
l[i] = "U"
elif l[i] is "G":
l[i] = "T"
elif l[i] is "H":
l[i] = "S"
elif l[i] is "I":
l[i] = "R"
elif l[i] is "J":
l[i] = "Q"
elif l[i] is "K":
l[i] = "P"
elif l[i] is "L":
l[i] = "O"
elif l[i] is "M":
l[i] = "N"
elif l[i] is "N":
l[i] = "M"
elif l[i] is "O":
l[i] = "L"
elif l[i] is "P":
l[i] = "K"
elif l[i] is "Q":
l[i] = "J"
elif l[i] is "R":
l[i] = "I"
elif l[i] is "S":
l[i] = "H"
elif l[i] is "T":
l[i] = "G"
elif l[i] is "U":
l[i] = "F"
elif l[i] is "V":
l[i] = "E"
elif l[i] is "W":
l[i] = "D"
elif l[i] is "X":
l[i] = "C"
elif l[i] is "Y":
l[i] = "B"
elif l[i] is "Z":
l[i] = "A"
print("".join(l))
我收到错误消息,说列表位置[i]
无效,因为它是一个字符串。但是,我的意图是使用值作为计数器来遍历列表对象并更改字母。
答案 0 :(得分:1)
from string import ascii_lowercase as lc,ascii_uppercase as uc,maketrans
transtab = maketrans(lc+uc,lc[::-1]+uc[::-1])
print("Hello".translate(transtab))
是一种方式(可能是首选方式)
from string import ascii_lowercase as lc,ascii_uppercase as uc,maketrans
translator = dict((letter1,letter2) for letter1,letter2 in zip(uc,uc[::-1]))
print("".join(translator.get(c,c) for c in "HELLO")
是使用字典的另一种方式
input_msg= "HELLO"
new_message = ""
for letter in input_msg:
new_message += chr(ord("Z")-(ord(letter)-ord("A")))
print (new_message)
是使用数学的另一种方式...... 基本上有一百万种方法可以实现这个目标
关于您的代码i
的if i=='A':...
如果您还想要索引(替换)
for index,letter in enumerate(l):
if letter == "A":
l[index] = "Z"
答案 1 :(得分:1)
您可以使用ord功能将字符映射到其unicode代码点。对于26个大写英文字母,这些字母对应于介于65和90之间的整数值。从155中减去每个字符的整数代码将为其提供补码的字符代码。
w = input('Enter a word\n').upper()
print(''.join(map(lambda c: chr(155 - ord(c)), w)))
答案 2 :(得分:1)
for i in l:
遍历l
中的字符,而不是数字索引。要使代码以最少的更改运行,您可以更改for
循环,如下所示。您还需要在循环外移动print
调用。
w = str.upper(input("Enter a word\n"))
l = list(w)
for i in range(len(l)):
if l[i] is "A":
l[i] = "Z"
elif l[i] is "B":
l[i] = "Y"
elif l[i] is "C":
l[i] = "X"
elif l[i] is "D":
l[i] = "W"
elif l[i] is "E":
l[i] = "V"
elif l[i] is "F":
l[i] = "U"
elif l[i] is "G":
l[i] = "T"
elif l[i] is "H":
l[i] = "S"
elif l[i] is "I":
l[i] = "R"
elif l[i] is "J":
l[i] = "Q"
elif l[i] is "K":
l[i] = "P"
elif l[i] is "L":
l[i] = "O"
elif l[i] is "M":
l[i] = "N"
elif l[i] is "N":
l[i] = "M"
elif l[i] is "O":
l[i] = "L"
elif l[i] is "P":
l[i] = "K"
elif l[i] is "Q":
l[i] = "J"
elif l[i] is "R":
l[i] = "I"
elif l[i] is "S":
l[i] = "H"
elif l[i] is "T":
l[i] = "G"
elif l[i] is "U":
l[i] = "F"
elif l[i] is "V":
l[i] = "E"
elif l[i] is "W":
l[i] = "D"
elif l[i] is "X":
l[i] = "C"
elif l[i] is "Y":
l[i] = "B"
elif l[i] is "Z":
l[i] = "A"
print("".join(l))
但是,有多更好的方法可以执行此操作,不需要26个if
/ elif
语句。
if l[i] == "A":
而不是if l[i] is "A":
。
这是一种改进的方法,希望不会太为您提升。它使用扩展切片来创建字母的反转版本。它使用zip
函数迭代一对字符串; zip
可用于任意数量的字符串,也可用于列表等其他容器。它使用字典来存储转换表。这个版本处理上部和上部小写字母。
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
rev_letters = letters[::-1]
# Build translation dictionary
trans = {}
# Loop over both letter strings in parallel
for u, v in zip(letters, rev_letters):
trans[u] = v
trans[u.lower()] = v.lower()
words = input("Enter some words: ")
lst = []
for ch in words:
# Get the translated version of ch if it exists,
# otherwise return ch
newch = trans.get(ch, ch)
# Add it to the list
lst.append(newch)
print("".join(lst))
Enter some words: Wizard abc
Draziw zyx