程序加载文件但没有更改任何值
Posted
技术标签:
【中文标题】程序加载文件但没有更改任何值【英文标题】:Program loading files however not having any values changed 【发布时间】:2015-04-06 18:33:50 【问题描述】:这是一个基本程序,应该从预制文件中获取字符串或获取用户输入,然后对其进行加密并使用凯撒密码将其保存到文件中。
我的问题是,由于某种原因,当我运行程序并选择“加载文件”选项时,它会遍历所有代码而没有任何错误消息,但是 .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()
【问题讨论】:
【参考方案1】:caeser(load_file())
添加一个保存文件...
【讨论】:
【参考方案2】:load_file 没有进行写入,因此您可以执行您为写入选项所做的操作
save_file(load_file())
或者调用load函数里面的save
save_file(text)
【讨论】:
以上是关于程序加载文件但没有更改任何值的主要内容,如果未能解决你的问题,请参考以下文章