現在のプログラムには、ユーザー入力を受け取り、シフトする量を選択できるようにして暗号化し、ファイルに保存するという問題があります。不明な理由で、私のプログラムはファイルを保存できるようですが、暗号化が完全に失われています。どんな助けでも大歓迎です。
text = "abcdefghijklmnopqrstuvwxyz 1234567890-=!£%^&*"
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()
caeser(text)
save_file()
elif ans == "r":
text = input("What is your file name you want to enter").lower()
caeser(text)
# 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
# save file shouldnt return text
def save_file(text):
name = input("Enter filename")
file = open(name, "w")
file.write(text)
file.close()
#load file shouldnt recieve a parameter
# error protection needs to be added
def load_file(text):
name = input("Please enter a file name")
file = open(name, "r")
text = file.read()
file.close()
return text