gpt4 book ai didi

python - 如何让python只读取一次文本文件的内容

转载 作者:行者123 更新时间:2023-12-01 07:00:07 27 4
gpt4 key购买 nike

我试图弄清楚如何让 Python 只读取 .txt 文件的内容一次。这是一个使用所有可打印 ASCII 字符来加密和解密消息的类项目。不需要这样做。这只是我用 Python 编写的第四个程序,我真的不知道自己在做什么,但我喜欢尝试想出不同的方法来完成作业。我希望我在这里正确输入了所有内容。我知道这是一个简单的解决方法,但我一直找不到答案。该任务将在接下来的 6 周内持续进行。我的计划是使加密更加复杂(是的,我知道你永远不应该使用 Python 进行加密)。所以我写这篇文章时有一个更大的愿景。

话虽这么说。如果有人想超越回答我的问题,请随意把整个事情拆开。让我知道我做错了什么,为什么,我怎样才能做得更好。我很想得到一些反馈。

import random
print("1. Encrypt")
print("2. Decrypt")
print(" ")

selection = int(input("What would you like to do? [1,2]? "))

while selection == 1:
plainText = input('Enter the message you wish to encrypt: ')

# right now the program encrypts the string at random between 1 and 95.
# All of the printable ASCII characters.
# the code below is written for when I can take the parameters of 1-95
# off and any input will simply loop around.
distance = random.randint(1, 95)
if distance < 1 or distance > 95:
print('Try Again')
continue
else:

# saves the random integer or (key)
# directly to a file without the user seeing it.

f = open('..\\Desktop\\encryptPractice\\theKey.txt', 'w+')

for key in range(1):
number = distance
f.write(str(number))

f.close()

code = ""

for ch in plainText:
ordvalue = ord(ch)
ordvalue = ordvalue + distance

while ordvalue < 32:
ordvalue += 95
while ordvalue > 126:
ordvalue -= 95

code += chr(ordvalue)

# saves the encrypted message
# directly to a file without the user seeing it.

f = open('..\\Desktop\\encryptPractice\\theMessage.txt', 'w+')

for theMessage in range(1):
secret = code
f.write(str(secret))
f.close()

print('Your message has been saved to the file named theMessage.txt')
break


# This is the decryption block - OPTION
# 2.)*********************************************

while selection == 2:

"""
I want to simply be able to open the file with the 'encrypted'
message on it and then open the file with the 'key' on it and
have the program decrypt the message and save it back to the
same file.

Both of the solutions below cause the program to read the
'encrypted' message over and over and over and...you get it.
"""

f = open('..Desktop\\encryptPractice\\theMessage.txt','r')
for line in f:
print(line)


f = open('..Desktop\\encryptPractice\\theMessage.txt','r')
while True:
line = f.readline()
if line == ""
break
print(line)

最佳答案

很难理解您的代码,因为它没有正确缩进。但这是我的猜测:行 while selection == 1:while selection == 2:使代码块循环运行,直到 selection 的值变化。如果您正在寻找if selection == 1:if selection == 2: .

现在有一些关于您共享的代码的其他评论:

  • distance这是不可能的小于 1 或大于 95,因为 randint(1, 95)返回 [1, 95] 范围内的整数。
distance = random.randint(1, 95)
if distance < 1 or distance > 95:
print('Try Again')
continue
  • 您正在使用 for迭代大小为 1 的序列 ( range(1) ) 的循环:
for theMessage in range(1):
secret = code
f.write(str(secret))

这个 block 可以减少到 f.write(str(code))

关于python - 如何让python只读取一次文本文件的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58677184/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com