有没有办法将for语句内容的内容传输到函数?

发布于 2025-01-21 07:43:40 字数 926 浏览 1 评论 0原文

def encrypt():
  while True:
    try:
        userinp = input("Please enter the name of a file: ")
        file = open(f"{userinp}.txt", "r")
        break  
    except:
      print("That File Does Not Exist!")
  second = open("encoded.txt", "w")
  for line in file:
    swapped(line)
    break
  #second.write()  
 
def swapped(line):
  newword = ""
  arranged = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
  random = ["q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"]
  print(line)

因此,我目前正在尝试制作一个程序来加密,然后解密文件。对于加密过程,我试图将给定文件的一行移动到另一个函数,该函数将从与文件中的单词相对应的一个索引中替换一个索引,并在同一索引中使用字母在随机列表中。

到目前为止功能,整个列表被打印出9次,这是给定文件中的行数。在上方,我试图做到这一点,以便在for循环下调用交换功能,但随后断开,该功能仅将文件的一行传输到第二个功能,但使列表正常打印出字母。

目前,我只需要以单独运载到交换函数的方式传输文件内容的帮助,而且还以列表正常工作的方式使我可以交换值。

我仍然是Python的新手,因此将不胜感激。

def encrypt():
  while True:
    try:
        userinp = input("Please enter the name of a file: ")
        file = open(f"{userinp}.txt", "r")
        break  
    except:
      print("That File Does Not Exist!")
  second = open("encoded.txt", "w")
  for line in file:
    swapped(line)
    break
  #second.write()  
 
def swapped(line):
  newword = ""
  arranged = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
  random = ["q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"]
  print(line)

So i'm currently trying to make a program that encrypts, then decrypts a file. For the encryption process i'm trying to move a line of the given file to another function, that will replace a letter at one index from the arranged list corresponding to the words in the file, with a letter at the same index, but isntead in the random list.

I've so far attempted to call the swapped function under the for loop that I created at the end of my while statement, and although successful in transferring everything within the file, when using a for loop for any of the given lists in the second function, the whole list gets printed out 9 times, which is the number of lines in the given file. Up above I tried to make it so that the swapped function is called under the for loop, but then breaks, which only transports one line of the file to the second function, but makes the lists print out the letters normally.

Right now I just need help with transferring the contents of my file in a way that each line is individually carried to swapped function, but also in a way that the lists work properly allowing me to swap values.

I'm still fairly new to python, so help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霓裳挽歌倾城醉 2025-01-28 07:43:40

如今,您交换功能只是打印输入,因为您实际上没有使用已安排的随机列表。我建议将列表更改为词典:

def encrypt():
  while True:
    try:
        userinp = input("Please enter the name of a file: ")
        file = open(f"{userinp}.txt", "r")
        break  
    except:
      print("That File Does Not Exist!")
  second = open("encoded.txt", "w")
  encrypted_lines = []
  #Creates a list of encrypted lines
  for line in file:
     encrypted_line = swapped(line)
     encrypted_lines.append(encrypted_line)
  return encrypted_lines
  #TODO save to encrypted file instead of returning
 
def swapped(line):
    new_line = ""
    encrypt_dict =  {'a': 'q', 'b': 'w', 'c': 'e', 'd': 'r', 'e': 't', 'f': 'y', 'g': 'u', 'h': 'i',
    'i': 'o', 'j': 'p', 'k': 'a', 'l': 's', 'm': 'd', 'n': 'f', 'o': 'g', 'p': 'h',
        'q': 'j', 'r': 'k', 's': 'l', 't': 'z', 'u': 'x', 'v': 'c', 'w': 'v', 'x': 'b',
    'y': 'n','z': 'm'}
    for i in line:
        if i in list(encrypt_dict.keys()):
            new_line = new_line + encrypt_dict[i]
    return new_line
print(encrypt())

As is, your swapped function just prints the input as you haven't actually used the arranged and random lists. I'd suggest changing the lists to a dictionary :

def encrypt():
  while True:
    try:
        userinp = input("Please enter the name of a file: ")
        file = open(f"{userinp}.txt", "r")
        break  
    except:
      print("That File Does Not Exist!")
  second = open("encoded.txt", "w")
  encrypted_lines = []
  #Creates a list of encrypted lines
  for line in file:
     encrypted_line = swapped(line)
     encrypted_lines.append(encrypted_line)
  return encrypted_lines
  #TODO save to encrypted file instead of returning
 
def swapped(line):
    new_line = ""
    encrypt_dict =  {'a': 'q', 'b': 'w', 'c': 'e', 'd': 'r', 'e': 't', 'f': 'y', 'g': 'u', 'h': 'i',
    'i': 'o', 'j': 'p', 'k': 'a', 'l': 's', 'm': 'd', 'n': 'f', 'o': 'g', 'p': 'h',
        'q': 'j', 'r': 'k', 's': 'l', 't': 'z', 'u': 'x', 'v': 'c', 'w': 'v', 'x': 'b',
    'y': 'n','z': 'm'}
    for i in line:
        if i in list(encrypt_dict.keys()):
            new_line = new_line + encrypt_dict[i]
    return new_line
print(encrypt())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文