简体   繁体   中英

Issue using two loops in python

Hey currently I'm dealing with for loops issue with my code and wondering if any of you can give me any insight

I'm trying to print text from a 2 files, and kind of merged them when printing. Two of the files contain the same amount of lines.

Here's my code so far:

links = open("link.txt", "r")
names = open("name.txt", "r")

for link in links:
    for name in names:
        print(link.strip(), "=", name.strip())

The loop for the name actually working, but the link doesnt, it stuck on the first line

If the files have the same number of lines:

links = open("link.txt", "r")
names = open("name.txt", "r")

for link,name in zip(links,names):
    print(link.strip(), "=", name.strip())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM