我做了一些代码,基本上用一个字母替换另一个字母,但问题是它需要很多代码。
请告诉我如何缩短它。有没有办法我可以使用变量而不需要写这么多行?
def begin():
lock = input("Enter the passcode")
if lock == 'password':
start()
else:
print("Wrong passcode")
begin()
def start():
print("Put in each letter one by one")
print("Your message should be a 5 letter word")
letter1 = input("")
letter2 = input("")
letter3 = input("")
letter4 = input("")
letter5 = input("")
#letter 1
if letter1 == 'f':
letter1 = 'a'
elif letter1 == 'g':
letter1 = 'b'
elif letter1 == 'h':
letter1 = 'c'
elif letter1 == 'i':
letter1 = 'd'
elif letter1 == 'j':
letter1 = 'e'
elif letter1 == 'k':
letter1 = 'f'
elif letter1 == 'l':
letter1 = 'g'
elif letter1 == 'm':
letter1 = 'h'
elif letter1 == 'n':
letter1 = 'i'
elif letter1 == 'o':
letter1 = 'j'
elif letter1 == 'p':
letter1 = 'k'
elif letter1 == 'q':
letter1 = 'l'
elif letter1 == 'r':
letter1 = 'm'
elif letter1 == 's':
letter1 = 'n'
elif letter1 == 't':
letter1 = 'o'
elif letter1 == 'u':
letter1 = 'p'
elif letter1 == 'v':
letter1 = 'q'
elif letter1 == 'w':
letter1 = 'r'
elif letter1 == 'x':
letter1 = 's'
elif letter1 == 'y':
letter1 = 't'
elif letter1 == 'z':
letter1 = 'u'
elif letter1 == 'a':
letter1 = 'v'
elif letter1 == 'b':
letter1 = 'w'
elif letter1 == 'c':
letter1 = 'x'
elif letter1 == 'd':
letter1 = 'y'
elif letter1 == 'e':
letter1 = 'z'
#letter 2
if letter2 == 'f':
letter2 = 'a'
elif letter2 == 'g':
letter2 = 'b'
elif letter2 == 'h':
letter2 = 'c'
elif letter2 == 'i':
letter2 = 'd'
elif letter2 == 'j':
letter2 = 'e'
elif letter2 == 'k':
letter2 = 'f'
elif letter2 == 'l':
letter2 = 'g'
elif letter2 == 'm':
letter2 = 'h'
elif letter2 == 'n':
letter2 = 'i'
elif letter2 == 'o':
letter2 = 'j'
elif letter2 == 'p':
letter2 = 'k'
elif letter2 == 'q':
letter2 = 'l'
elif letter2 == 'r':
letter2 = 'm'
elif letter2 == 's':
letter2 = 'n'
elif letter2 == 't':
letter2 = 'o'
elif letter2 == 'u':
letter2 = 'p'
elif letter2 == 'v':
letter2 = 'q'
elif letter2 == 'w':
letter2 = 'r'
elif letter2 == 'x':
letter2 = 's'
elif letter2 == 'y':
letter2 = 't'
elif letter2 == 'z':
letter2 = 'u'
elif letter2 == 'a':
letter2 = 'v'
elif letter2 == 'b':
letter2 = 'w'
elif letter2 == 'c':
letter2 = 'x'
elif letter2 == 'd':
letter2 = 'y'
elif letter2 == 'e':
letter2 = 'z'
#letter 3
if letter3 == 'f':
letter3 = 'a'
elif letter3 == 'g':
letter3 = 'b'
elif letter3 == 'h':
letter3 = 'c'
elif letter3 == 'i':
letter3 = 'd'
elif letter3 == 'j':
letter3 = 'e'
elif letter3 == 'k':
letter3 = 'f'
elif letter3 == 'l':
letter3 = 'g'
elif letter3 == 'm':
letter3 = 'h'
elif letter3 == 'n':
letter3 = 'i'
elif letter3 == 'o':
letter3 = 'j'
elif letter3 == 'p':
letter3 = 'k'
elif letter3 == 'q':
letter3 = 'l'
elif letter3 == 'r':
letter3 = 'm'
elif letter3 == 's':
letter3 = 'n'
elif letter3 == 't':
letter3 = 'o'
elif letter3 == 'u':
letter3 = 'p'
elif letter3 == 'v':
letter3 = 'q'
elif letter3 == 'w':
letter3 = 'r'
elif letter3 == 'x':
letter3 = 's'
elif letter3 == 'y':
letter3 = 't'
elif letter3 == 'z':
letter3 = 'u'
elif letter3 == 'a':
letter3 = 'v'
elif letter3 == 'b':
letter3 = 'w'
elif letter3 == 'c':
letter3 = 'x'
elif letter3 == 'd':
letter3 = 'y'
elif letter3 == 'e':
letter3 = 'z'
#letter 4
if letter4 == 'f':
letter4 = 'a'
elif letter4 == 'g':
letter4 = 'b'
elif letter4 == 'h':
letter4 = 'c'
elif letter4 == 'i':
letter4 = 'd'
elif letter4 == 'j':
letter4 = 'e'
elif letter4 == 'k':
letter4 = 'f'
elif letter4 == 'l':
letter4 = 'g'
elif letter4 == 'm':
letter4 = 'h'
elif letter4 == 'n':
letter4 = 'i'
elif letter4 == 'o':
letter4 = 'j'
elif letter4 == 'p':
letter4 = 'k'
elif letter4 == 'q':
letter4 = 'l'
elif letter4 == 'r':
letter4 = 'm'
elif letter4 == 's':
letter4 = 'n'
elif letter4 == 't':
letter4 = 'o'
elif letter4 == 'u':
letter4 = 'p'
elif letter4 == 'v':
letter4 = 'q'
elif letter4 == 'w':
letter4 = 'r'
elif letter4 == 'x':
letter4 = 's'
elif letter4 == 'y':
letter4 = 't'
elif letter4 == 'z':
letter4 = 'u'
elif letter4 == 'a':
letter4 = 'v'
elif letter4 == 'b':
letter4 = 'w'
elif letter4 == 'c':
letter4 = 'x'
elif letter4 == 'd':
letter4 = 'y'
elif letter4 == 'e':
letter4 = 'z'
#letter 5
if letter5 == 'f':
letter5 = 'a'
elif letter5 == 'g':
letter5 = 'b'
elif letter5 == 'h':
letter5 = 'c'
elif letter5 == 'i':
letter5 = 'd'
elif letter5 == 'j':
letter5 = 'e'
elif letter5 == 'k':
letter5 = 'f'
elif letter5 == 'l':
letter5 = 'g'
elif letter5 == 'm':
letter5 = 'h'
elif letter5 == 'n':
letter5 = 'i'
elif letter5 == 'o':
letter5 = 'j'
elif letter5 == 'p':
letter5 = 'k'
elif letter5 == 'q':
letter5 = 'l'
elif letter5 == 'r':
letter5 = 'm'
elif letter5 == 's':
letter5 = 'n'
elif letter5 == 't':
letter5 = 'o'
elif letter5 == 'u':
letter5 = 'p'
elif letter5 == 'v':
letter5 = 'q'
elif letter5 == 'w':
letter5 = 'r'
elif letter5 == 'x':
letter5 = 's'
elif letter5 == 'y':
letter5 = 't'
elif letter5 == 'z':
letter5 = 'u'
elif letter5 == 'a':
letter5 = 'v'
elif letter5 == 'b':
letter5 = 'w'
elif letter5 == 'c':
letter5 = 'x'
elif letter5 == 'd':
letter5 = 'y'
elif letter5 == 'e':
letter5 = 'z'
print("Your encypted message is:")
print(letter1 + letter2 + letter3 + letter4 + letter5)
QUIT = input("Press enter to quit")
quit
begin()
答案 0 :(得分:2)
使用str.maketrans()
创建翻译表,str.translate()
使用翻译。使用string.ascii_lowercase
而不是拼写您的信件。使用string.ascii_letters
来处理大写和小写字母,这可能是一个很好的改进。
>>> import string
>>> letters = string.ascii_lowercase
>>> table = str.maketrans(letters[5:]+letters[:5], letters)
>>> str.translate('crown', table)
'xmjri'
或者将字典用作转换表。如果没有指定的映射,请使用dict.get()
和默认值来允许字符无法翻译。
>>> import string
>>> letters = string.ascii_lowercase
>>> d = dict(zip(letters[5:]+letters[:5], letters))
>>> ''.join(d.get(c, c) for c in 'crown')
'xmjri'
答案 1 :(得分:0)
我建议使用if语句创建一个包含从letter1
到letter2
的映射的字典。
letterMapping = {'f':'a','g':'b'}
等等,当您收到输入时,请执行以下操作:
letter1 = input("")
letterMapping[letter1]
因此如果letter1
为f
,则会返回a
。
答案 2 :(得分:0)
您好,为什么不尝试使用while循环和数组。
不确定代码是否有效,但您会明白
def start():
letters = array();
x = 0;
while(x != 5):
print("Enter 1 letter");
letters[x] = input("");
x = x +1;
while(x != 5):
if (letters[x] == 'f'):
letters[x] = 'a'
elif(letters[x] == 'g'):
letters[x] = 'b'
elif ((letters[x] == 'h'):
letters[x]= 'c'
elif (letters[x] == 'i'):
letters[x] = 'd'
elif (letters[x] == 'j'):
letters[x] = 'e'
elif (letters[x] == 'k'):
letters[x] = 'f'
elif (letters[x] == 'l'):
letters[x] = 'g'
elif (letters[x] == 'm'):
letters[x] = 'h'
elif (letters[x] == 'n'):
letters[x] = 'i'
elif (letters[x] == 'o'):
letters[x] = 'j' ;
elif (letters[x] == 'p'):
letters[x] = 'k'
elif (letters[x] == 'q'):
letters[x] = 'l'
elif (letters[x] == 'r'):
letters[x] = 'm'
elif (letters[x] == 's'):
letters[x] = 'n'
elif (letters[x] == 't'):
letters[x] = 'o'
elif (letters[x] == 'u'):
letters[x] = 'p'
elif (letters[x] == 'v'):
letters[x] = 'q'
elif (letters[x] == 'w'):
letters[x] = 'r'
elif (letters[x] == 'x'):
letters[x] = 's'
elif (letters[x] == 'y'):
letters[x] = 't'
elif (letters[x] == 'z'):
letters[x] = 'u'
elif (letters[x] == 'a'):
letters[x] = 'v' }
elif (letters[x] == 'b'):
letters[x] = 'w'
elif (letters[x] == 'c'):
letters[x] = 'x'
elif (letters[x] == 'd'):
letters[x] = 'y'
elif (letters[x] == 'e'):
letters[x] = 'z'
x = x + 1;
print("Your encypted message is:")
print(letters[0] + letters[1] + letters[2] + letters[3] + letters[4])
QUIT = input("Press enter to quit")
quit