我目前正在尝试制作一个简单的程序,该程序将吸收用户的输入并吐出相同的内容,但基于简单的转换表使用不同的字母/字符。
Ex:可以说以下是转换;左边是用户的输入,右边是程序的输出。
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(
)
答案 0 :(得分:0)
在函数中尝试此操作或类似操作,可以避免一起输出str。如果还要处理小写字母,请检查chr()整数值,如果大于96,则减去32:
def convert(inputString):
outputString = ""
for letter in inputString:
if ord(letter) >= 65 and ord(letter) < 86:
outputString += letter + chr((ord(letter) + 1))
elif ord(letter) >= 97 and ord(letter) < 122:
outputString += chr(ord(letter) - 32) + chr(ord(letter) - 31)
elif letter == 'z' or letter == 'Z':
outputString += "ZA"
else:
outputString += letter
return outputString
wordToConvert = "ABC abc"
print(convert( wordToConvert))
wordToConvert = "Hello there"
print(convert( wordToConvert))
输出:
ABBCCD ABBCCD
HIEFLMLMOP TUHIEFRSEF
https://docs.python.org/2/library/functions.html#chr
https://www.quora.com/What-is-difference-in-the-ASCII-values-of-uppercase-and-lowercase-letters