有没有办法将for语句内容的内容传输到函数?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如今,您交换功能只是打印输入,因为您实际上没有使用已安排的随机列表。我建议将列表更改为词典:
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 :