0

I have a text file with a list of 4 digit codes inside of it as such:

4317
2352
2315

At a point in my code I ask users to input there 4 digit code, what I want to do is to then match it so if they say their code is 4317 it gives me 0 and then I want to use that number to find something in a second list that is full of names as such:

John
Jen
James
Joe

So if I type in 4317 it gives me 0 and then I want to print what number 0 thing is in the second list. I am completely stumped on how to do this, both lists are in separate .txt files.

1
  • 1
    Would you like to edit your question to include the python code you're using? Commented Apr 8, 2018 at 14:32

2 Answers 2

1

You can do something like this:

with open('first.txt') as  f, open('second.txt') as f2:
    lines = f.readlines()
    lines_2 = f2.readlines()
    lns = [line.strip() for line in lines]
    lns_2 = [line.strip() for line in lines_2]


lns = list(map(int, lns))
lns_2 = list(map(int, lns_2))

n = int(input('Enter a number: '))
if n in lns:
    print(lns.index(n))
    print(lns_2[lns.index(n)])
Sign up to request clarification or add additional context in comments.

Comments

0

Open both files, loop over each line together using zip() (assuming each file has same number of lines)

usercode = input("code:")
with open("f1.txt") as codes, open("f2.txt") as names:
    for code, name in zip(codes, names):
        if code == usercode:
            print(name)
            break

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.