python文本写入不循环_Python在每次while循环重复后写入文本文件的新行

我正在尝试将基本加密程序的输出保存到文本文件中。它是有效的,问题是它将每个新的输出保存在最后一个输出旁边,而没有空格或新行(我想要的)。在

我希望用户能够选择一行读取和解密,而不是解密整个批。在

现在我得到这个-This program can run three different sub-programs-

1- Run the encryption and decryption sub-program specified in Unit A453- CAM 3.

2- Run a test which encrypts and decrypts each ascii character with each other ascii character.

3- Run a test which generates random inputs and keywords, before encrypting and decrypting them.

Please choose either 1, 2 or 3- 1

Running text based program-

Do you want to encrypt or decrypt? encrypt

Input a key- abc

Input a second key- 123

Input a string to encrypt- Theo

Your encrypted text is %vuA -it has been saved.

Do you wish to continue? Y/N- y

Do you want to encrypt or decrypt? encrypt

Input a key- 123

Input a second key- abc

Input a string to encrypt- Theo

Your encrypted text is %vuA -it has been saved.

Do you wish to continue? Y/N- y

Do you want to encrypt or decrypt?

正如你所看到的,我已经做了两次同样的事情(忽略这样一个事实,即反转加密密钥没有任何作用——我实际上没有为它们编写一个合适的算法,它只是把它们加在一起)

文本文件如下-

^{pr2}$

我想让它来做这个-%vuA

%vuA

最后还有一件事。与其说是个问题,倒不如说是一个“他妈的,为什么?!”是在“do you wish continue”中选择“Y”或“N”之前,文本不会写入文本文件。在

执行读写操作的特定代码是这个函数-def User_Text_Interface(Repeat):

while Repeat == True:

f = open("COT.txt", "a+")

ED, Key, Key2, Temp = input("Do you want to encrypt or decrypt? "), input("Input a key- "), input("Input a second key- "), 0

if ED.lower() =="encrypt" or ED.lower() == "e":

User_Input = input("Input a string to " + str(ED) + "- ")

Key, Key2 = Compatibility(Key, User_Input), Compatibility(Key2,User_Input)

if ED.lower() == "encrypt" or ED.lower() == "e":

ET = str(Encrypt((Encrypt(User_Input, Key)), Key2))

f.write(ET)

print("Your encrypted text is " + ET + " -it has been saved.")

elif ED.lower() == "decrypt" or ED.lower() == "d":

with open("COT.txt", "r+") as f:

for line in f:

print(str(Decrypt((Decrypt((Encrypt((Encrypt(User_Input, Key)), Key2)), Key2)), Key)))

Repeat = input("Do you wish to continue? Y/N- ")

if Repeat.lower() == "yes" or Repeat.lower() == "y":

Repeat = True

else:

Repeat = False

我剩下的代码,大部分都可以忽略,因为它是多余的-import time, sys, random

Master_Key = "0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#£$%&'()*+,-./:;?@[\\]^_`{|}~"

def Encrypt(User_Input, Key):

Output = ""

for i in range(len(User_Input)):

Ref_For_Output = Master_Key.index(User_Input[i]) + Master_Key.index(Key[i])

if Ref_For_Output >= len(Master_Key):

Ref_For_Output -= len(Master_Key)

Output += Master_Key[Ref_For_Output]

return Output

def Decrypt(User_Input, Key):

Output = ""

for i in range(len(User_Input)):

Ref_For_Output = Master_Key.index(User_Input[i]) - Master_Key.index(Key[i])

if Ref_For_Output < 0:

Ref_For_Output += len(Master_Key)

Output += Master_Key[Ref_For_Output]

return Output

def Ordered_Test_Algorithm(Null):

for i in range(len(Master_Key)-1):

Input= Master_Key[i]

print("Input = " + Input)

for i in range(len(Master_Key)-1):

Key = Master_Key[i]

for i in range(len(Master_Key)-1):

Key2 = Master_Key[i]

Output = Decrypt(Encrypt(Input, Key, Key2), Key, Key2)

print("Encryption and decryption of Input- " + str(Input) + " with the Key- " + str(Key) + " and a second Key of " + str(Key2) + " results in an output of " + str(Output))

if Input == Output:

print("Pass")

else:

print("Fail")

sys.exit

print("Testing complete- Pass")

def Random_Test_Algorithm(Input_Length, Repeat_times):

for i in range(Repeat_times):

User_Input, Key, Key2 = "", "", ""

for i in range(Input_Length):

Input_ref, Key_ref, Key_2_Ref = random.randint(0, len(Master_Key)-1), random.randint(0, (len(Master_Key)-1)), random.randint(0, (len(Master_Key)-1))

User_Input += Master_Key[Input_ref]

Key += Master_Key[Key_ref]

Key2 += Master_Key[Key_2_Ref]

print("The randomly generated " + str(Input_Length) + " character input key and second key are " + User_Input + ", " + Key + " and " + Key2 +" respectively.")

print("The result of encryption is- " + Encrypt(User_Input, Key, Key2) )

print("The result of decryption is- " + Decrypt(Encrypt(Input, Key, Key2), Key, Key2) )

if User_Input == Decrypt(Encrypt(Input, Key, Key2), Key, Key2):

print("The encryption and decryption of " + User_Input + " with " + Key + " and " + Key2 + " was successful")

else:

print("The encryption and decryption of " + User_Input + " with " + Key + " and " + Key2 + " was un-successful")

sys.exit

def Compatibility(Key, User_Input):

Temp = 0

while Key == "":

print("Your key cannot be blank")

while len(Key) > len(User_Input):

Key = Key[:-1]

while len(Key) < len(User_Input):

Key += (Key[Temp])

Temp += 1

return Key

def User_Text_Interface(Repeat):

while Repeat == True:

f = open("COT.txt", "a+")

ED, Key, Key2, Temp = input("Do you want to encrypt or decrypt? "), input("Input a key- "), input("Input a second key- "), 0

if ED.lower() =="encrypt" or ED.lower() == "e":

User_Input = input("Input a string to " + str(ED) + "- ")

Key, Key2 = Compatibility(Key, User_Input), Compatibility(Key2,User_Input)

if ED.lower() == "encrypt" or ED.lower() == "e":

ET = str(Encrypt((Encrypt(User_Input, Key)), Key2))

f.write(ET)

print("Your encrypted text is " + ET + " -it has been saved.")

elif ED.lower() == "decrypt" or ED.lower() == "d":

with open("COT.txt", "r+") as f:

for line in f:

print(str(Decrypt((Decrypt((Encrypt((Encrypt(User_Input, Key)), Key2)), Key2)), Key)))

Repeat = input("Do you wish to continue? Y/N- ")

if Repeat.lower() == "yes" or Repeat.lower() == "y":

Repeat = True

else:

Repeat = False

print("This program can run three different sub-programs-")

print("1- Run the encryption and decryption sub-program specified in Unit A453- CAM 3.")

print("2- Run a test which encrypts and decrypts each ascii character with each other ascii character.")

print("3- Run a test which generates random inputs and keywords, before encrypting and decrypting them.")

Option = input("Please choose either 1, 2 or 3- ")

if Option == "1":

print("Running text based program-")

time.sleep(1)

User_Text_Interface(True)

elif Option == "2":

print("This test will encrypt and decrypt each keyboard character with every other keyboard character")

print("It will print around 1,860,000 lines of output, unless a decrypted value is not equal to its input, this will cause the test to stop")

print("Beginning test- ")

Ordered_Test_Algorithm("Null")

time.sleep(1)

elif Option == "3":

print("This test will generate a random input and keyword of a specified length using the random.randint function in the random module.")

print("It will then encrypt and decrypt the input with the keyword before checking if the output is equal to the input.")

print("The test will repeat a specifieed number of times.")

Input_Length = int(input("Input a numerical length (Length in characters e.g. 'Python' is 6 characters)for the key and keyword- "))

Repeat_times = int(input("Input the number of times the test should be repeated- "))

print("Beginning test- ")

time.sleep(1)

Random_Test_Algorithm(Input_Length, Repeat_times)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值