如何让python只读取

2024-05-18 22:20:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图找出如何让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)

Tags: andthetotxtmessageforlineit
1条回答
网友
1楼 · 发布于 2024-05-18 22:20:40

很难理解您的代码,因为它没有正确缩进。但我猜: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))

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

相关问题 更多 >

    热门问题