这是一个基本程序,它应该从预制文件中获取字符串或获取用户输入,然后对其进行加密并使用凯撒密码将其保存到文件中。
我的问题是,由于某种原因,当我运行程序并选择“加载文件”选项时,它会遍历所有代码而没有任何错误消息,但是 .txt 文件上的内容不会被加密,有人知道怎么做吗解决这个问题?
要获得更简洁的代码布局,请访问 Pastebin 链接: http://pastebin.com/dJJ1M4g7
def main():
#if they want to save the file after the encrypting if statement
ans = input("Would you like to save to a file of read a file, press w or r").lower()
if ans == "w":
text = input("What is your text you want to enter").lower()
save_file(caeser(text))
elif ans == "r":
caeser(load_file())
# organise loop & function
def caeser(text):
shift = int(input("How much would you like to shift?: "))
shifted_list = []
for letter in text:
character_lower = letter.lower()
ASCII = ord(character_lower)
shift = shift % 26
shifted_letter = ASCII + shift
shifted_char = chr(shifted_letter)
shift_loop = shifted_letter - 26
shift_loop_char = chr(shift_loop)
if shifted_letter >= 97 and shifted_letter <= 122:
shifted_list.append(shifted_char)
text = ''.join(shifted_list)
elif shift_loop >= 97 and shift_loop <= 122:
shifted_list.append(shift_loop_char)
text = ''.join(shifted_list)
else:
shifted_list.append(character_lower)
text = ''.join(shifted_list)
encrypted = text
return encrypted
def save_file(text):
name = input("Enter filename")
file = open(name, "w")
file.write(text)
file.close()
# error protection needs to be added
def load_file():
name = input("what is your file name? (include .txt)")
file = open(name, "r")
text = file.read()
file.close()
return text
main()