如何用Python编写ASCII Caesar密码
我是一个完全的初学者,我的任务是创建一段如何用Python编写ASCII Caesar密码,python,python-3.x,ascii,caesar-cipher,Python,Python 3.x,Ascii,Caesar Cipher,我是一个完全的初学者,我的任务是创建一段 要求用户输入一个字符串 要求用户输入一个介于1和25之间的值,该值可用作ascii移位器 输出他们的字符串输入,基于他们希望它以ascii形式移位的值 我知道这是一个远大的目标,但这里是我的代码到目前为止,任何帮助或在正确的方向上的指针将不胜感激 # Description: # A program to read in a single word message # the input is modified by code and the Enco
# Description:
# A program to read in a single word message
# the input is modified by code and the Encoded
# word is displayed.
# NAMED_CONSTANTS
SMALLEST_SHIFT_NUMBER = 1
LARGEST_SHIFT_NUMBER = 25
ASSCII_CODE_NINETY = 90
ASSCII_CODE_TWENTY_SIX = 26
# DECLARE the Program Variables
# Set the initial SHIFT number to add on as zero
# This number is ADDED to a letter's ASCII code value
# Default initial value is: 0
code_number_add_on = 0
# A capital letter entered by the user
# Default initial value is: A
capital_letter = "A"
# The ASCII CODE for the new letter that replace
# a user's capital letter
# Default initial value is: 0
ascii_code_new_letter = 0
# The new LETTER that replace a user's capital letter
# Default initial value is: A
new_letter = "A"
# function get_encoding_number returns an int
# value after input validation for
# a value in the range:
def get_encoding_number():
# Set a flag for input validation
# Default initial value is: False
valid_shift_number_entered = False
# While the flag is False - prompt for and process user input
while valid_shift_number_entered == False:
# Prompt for a valid number
get_encoding_number = int(input("Enter a value on the range 1 to 25: "))
# Test the user input value
if get_encoding_number >= SMALLEST_SHIFT_NUMBER and get_encoding_number <= LARGEST_SHIFT_NUMBER:
# Valid input - update the flag to True
valid_shift_number_entered = True
else:
# Invalid input - display an error message
print("Error: unacceptable value.")
# END if statement
# END while loop
return 2
# END function first
# function string_to_be_encoded returns a String
# value. The function prints a user prompt and
# returns the input without input validation
def function_to_get_user_word_for_encoding():
string_to_be_encoded = input("Enter a word in CAPITAL letters to be encoded. Press ENTER. ")
return string_to_be_encoded
# END function second
# a helper function encode_one_letter
# returns an
# encoded letter.
#
def encode_one_letter( the_letter, encode_number ):
# code from alphabet_08.py
# approx lines 63 to 78
# Use the function ord to determine the
# ASCII code for the letter entered by the user
ascii_code_for_user_letter = ord( the_letter )
# Now the SHIFT value must be added
ascii_code_new_letter = ascii_code_for_user_letter + encode_number
# if the calculated value is greater than 90 then
# subtract 26 to create a valid capital letter value
if ascii_code_new_letter > ASSCII_CODE_NINETY:
ascii_code_new_letter = ascii_code_new_letter - ASSCII_CODE_TWENTY_SIX
# END if
# Use the function ord to determine the ASCII code for
# the new letter after the SHIFT value has been added.
new_letter = chr(ascii_code_new_letter)
return new_letter
# END function encode_one_letter
# function returns a String
# value. The function has two parameters
# a number and a text string. The function
# encodes each letter in the text and returns
# the encoded word as a String.
def third( number_for_code, word ):
# a local variable to hold an encoded letter
encoded_letter = ""
# a local variable to hold the encoded word
encoded_word = ""
# a for loop to encode each letter in the
for letter in word:
# encode the current letter
encoded_letter = encode_one_letter( letter , number_for_code )
# add the letter to the encoded word using the method append()
encoded_word = encoded_word + encoded_letter
# reset the encoded_letter to an empty string
encoded_letter = ""
# END for loop
# return the encoded word
return encoded_word
# END function third
# function main prints User inputs
def main():
# print a welcome message
print("\n\tWelcome to program outline_03.py")
# read a valid number input
# into a local variable
# ??? = get_encoding_number()
# read a text input
# into a local variable
# ??? = second()
# combine the user inputs
# into a third local variable
v3 = third( 2, "PAUL" )
print("\n\tHere is the encoded word:", v3 )
print("\n\tProgram secret_codes.py ends.")
# END function main
# call function main
main()
#说明:
#读入单字信息的程序
#输入由代码和编码的
#将显示word。
#命名常数
最小移位数=1
最大移位数=25
关联代码=90
ASSCII_代码_二十_六=26
#声明程序变量
#将要添加的初始班次号设置为零
#此数字将添加到字母的ASCII码值中
#默认初始值为:0
代码\u编号\u添加\u on=0
#用户输入的大写字母
#默认初始值为:A
大写字母=“A”
#替换的新字母的ASCII码
#用户的大写字母
#默认初始值为:0
ascii码新字母=0
#替换用户大写字母的新字母
#默认初始值为:A
新字母=“A”
#函数get_encoding_number返回一个整数
#输入验证后的值
#范围内的值:
def get_编码_编号():
#设置输入验证的标志
#默认初始值为:False
有效的\u班次\u编号\u输入=错误
#当标志为False时-提示并处理用户输入
有效的\u移位\u编号\u输入==假:
#提示输入有效数字
get_encoding_number=int(输入(“在1到25的范围内输入一个值”))
#测试用户输入值
如果get_encoding_number>=最小的移位数和get_encoding_number ASSCII_CODE:
ascii码新字母=ascii码新字母-ASSCII码二十六
#如果结束
#使用函数ord确定
#添加移位值后的新字母。
新字母=chr(ascii码新字母)
回信
#结束函数编码一个字母
#函数返回一个字符串
#价值观。该函数有两个参数
#一个数字和一个文本字符串。功能
#对文本中的每个字母进行编码并返回
#以字符串形式编码的单词。
def第三(数字表示字母代码,单词):
#保存编码字母的局部变量
encoded_letter=“”
#保存编码字的局部变量
encoded_word=“”
#一个for循环,用于对中的每个字母进行编码
对于大写字母:
#对当前字母进行编码
编码字母=编码一个字母(字母、数字表示字母代码)
#使用append()方法将字母添加到编码的单词中
编码字=编码字+编码字母
#将编码的字母重置为空字符串
encoded_letter=“”
#循环结束
#返回编码字
返回编码字
#第三端功能
#函数main打印用户输入
def main():
#打印欢迎信息
打印(“\n\t导入程序大纲\u 03.py”)
#读取输入的有效数字
#转化为局部变量
# ??? = 获取\u编码\u编号()
#阅读文本输入
#转化为局部变量
# ??? = 第二()
#合并用户输入
#转换为第三个局部变量
v3=第三(2,“保罗”)
打印(“\n\n有编码的单词:”,v3)
打印(“\n\t程序机密\u code.py结束。”)
#端功能主
#调用函数main
main()
您的代码中有很多神奇的数字,函数名(如third(…)
)不能告诉您函数的功能,还有大量过时的注释,解释了一些您可以避免解释的东西,如果您有更好的变量名开始-等等
让您的生活变得轻松,使用python提供的功能:
- 或
from string import ascii_lowercase as lc
while True:
# simply force the input to be lower or upper, no cooperation needed
text = input ("A message, enter to quit: ").lower()
if not text:
break
try:
# use try except in case they do not input a number and allow -25 to 25
# to enable back-converting your text
shift = int(input("Shift between -25 and 25: "))
if not (-25 <= shift <= 25):
raise ValueError()
except ValueError:
print("Try again")
# make the translation dictionary and translate the text
tran = str.maketrans(lc, lc[shift:] + lc [:shift])
print(text.translate(tran),"\n")
这看起来像是一个家庭作业问题,你应该自己解决
A message, enter to quit: Baker Street23
Shift between -25 and 25: 5
gfpjw xywjjy23
A message, enter to quit: gfpjw xywjjy23
Shift between -25 and 25: -5
baker street23