這是一個基本的程序,應該從預製文件或採取用戶輸入字符串,然後加密並使用凱撒密碼將其保存到文件。程序加載文件,但沒有任何值更改
我的問題是,由於某種原因,當我運行該程序並選擇「加載文件」選項時,它將經過所有代碼而沒有任何錯誤消息,但是.txt文件上的寫入不會被加密,有誰知道如何解決這個問題?
對於代碼的簡潔佈局這裏有一個引擎收錄鏈接: 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()