【发布时间】:2018-09-27 18:53:33
【问题描述】:
我目前正在尝试制作一个简单的程序,该程序将接受用户的输入并根据简单的转换表输出相同的内容,但使用不同的字母/字符。
例如:假设以下是转换;左边是用户的输入,右边是程序的输出。
A=AB
B=BC
C=CD
D=DE
etc...
我想让程序取一个句子或短语并吐出转换后的版本:
例如:这就是我想要的最终产品。
Input = Hello there
Output = HIEFLMLMOP TUHIEFRSEF
我必须承认,我对 Python 的经验很少。自从我在高中上编程课以来已经有 4 年多的时间了,我们在 3.0 之前学习了一个旧版本,所以有些东西似乎是新的/不同的。非常感谢任何帮助!
编辑:
import sys
'a' == "AB"
'b' == "BC"
'c' == "CD"
'd' == "DE"
'e' == "EF"
'f' == "FG"
'g' == "GH"
'h' == "HI"
'i' == "IJ"
'j' == "JK"
'k' == "KL"
'l' == "LM"
'm' == "MN"
'n' == "NO"
'o' == "OP"
'p' == "PQ"
'q' == "QR"
'r' == "RS"
's' == "ST"
't' == "TU"
'u' == "UV"
'v' == "VW"
'w' == "WX"
'x' == "XY"
'y' == "YZ"
'z' == "ZA"
usrinpt = input('Enter what you would like to encode:');
print("Generated Product:");
if input(usrinpt) == a:
print ("AB")
def main():
input("Press enter and exit")
编辑 2:
尝试了@wwii 的建议,但似乎不知道我如何让程序吐出转换。
import sys
translate = {"A": 'AB', "B": 'BC', "C": 2, "D": 3, "E": 4, "F": 5, "G": 6, "H": 7, "I": 8, "J": 9, "K": 10, "L": 11, "M": 12, "N": 13, "O": 14, "P": 15, "Q": 16, "R": 17, "S": 18, "T": 19, "U": 20, "V": 21, "W": 22, "X": 23, "Y": 24, "Z": 25, " ": 26}
conversion = input("What would you like cypher?: ").upper()
print("Here is the output: "conversion)
编辑 3: 找到了一些似乎非常接近我正在寻找的代码。只是很难让它阅读我的翻译。
key = 0
translate = {'a':'AB' , 'b':'BC'}
#going to add more to the translate list once I get those letters working
def wub():
def choice():
choice = input("Do you wish to Encrypt of Decrypt?")
choice = choice.lower()
if choice == "e" or "encrypt":
return choice
elif choice == "d" or "decrypt":
return choice
else:
print("Invalid response, please try again.")
choice()
def message():
user = input("Enter your message: ")
return user
def waffle(choice, message, key):
translated = ""
if choice == "e" or "encrypt":
for character in message:
num = ord(character)
num += key
translated += chr(num)
derek = open('Encrypted.txt', 'w')
derek.write(translated)
derek.close()
return translated
else:
for character in message:
num = ord(character)
num -= key
translated += chr(num)
return translated
choice = choice() #Runs function for encrypt/decrypt selection. Saves choice made.
message = message() #Run function for user to enter message. Saves message.
final = waffle(choice, message, key) #Runs function to translate message, using the choice, message and key variables)
print("\n Operation complete!")
print(final)
wub(
)
【问题讨论】:
-
嗯,你一定尝试过一些东西......你有一些不起作用的代码吗?检查[SO]: How to create a Minimal, Complete, and Verifiable example (mcve)。此外,Z 转换为 ZA?
-
预期的输出对我来说不是很清楚。另外,我相信你也可以在旧版本中做你想做的事情。展示你的尝试,如果你被困在某个地方,有人会帮助你
-
一个好的开始是创建一个函数,它接收由单个字符组成的字符串,并使用
ord和chr函数和string.upper()方法一起返回一个字符应该被替换的字符串。 -
@mad_ 字符 x 映射到字符 X,X+1 但不清楚 z/Z 应该映射到什么...
-
我认为 Z 应该映射到 A。看看 Caesar_cipher 如何处理这些情况。
标签: python python-3.x encoding type-conversion