0

The following code searches a text file for a name and displays the related number in a tkinter entry box in Python.

so original text file includes:

bob 19
dan 20
shayne 17

I would like add another nested loop so that if there are two names the same then two numbers are returned to the entry box. Sorry, I am new to Python, have tried but always come up with an error.

bob 18
bob 19
dan 20
shayne 17
#https://www.youtube.com/watch?v=lR90cp1wQ1I

from tkinter import *
from tkinter import messagebox

race = []

def displayInfo(race, name):
    found = False
    pos = 0
    while pos < len(race) and not found:
        if race[pos][0] == name: 
            found = True
        pos+=1 
    if found:
        return race[pos-1][1]   
    else:
        messagebox.showerror(message = "Invalid input, please try again.")

def clickArea():
    fin.set(displayInfo(race, name.get()))

def createlist():
    raceFile = open ("C:/python/files/number_list.txt", 'r')
    for line in raceFile:
        racer = line.split()
        race.append(racer)
    raceFile.close()
    return race

root = Tk()
root.title("Read From text File/List GUI")

Label(root, text="Name").grid(row=0, column=0)
name = StringVar()
Entry(root, textvariable=name).grid(row=0, column =1)

Label(root, text="Finish Time").grid(row=2, column=0)
fin=IntVar()
Label(root, textvariable=fin).grid(row=2, column=1)

button = Button(root, text="Finish Time", command=clickArea)
button.grid(row=3, column=0, columnspan=2)

createlist()
print(race)

2 Answers 2

1

your question is not related to tkinter, so I made the code without it.

It works like this: you enter the name you're looking for, then it looks for matches using the count method. If there is a match, then the index is written to the 'B' array. Further, since there is a space between the name and number, we take the space index + 1 and start outputting the string from this position to the end.

name = input('who do you want to find: ') + " "


with open("number_list.txt", "r") as file:
    A = file.readlines()


#remove program entry '\n'   
for i in range(len(A)):
    A[i] = A[i].strip()


#getting matching names
B = [] #the court records the names we need
for i in A:
    if i.count(name): #truth check
    #this notation is equivalent to the notationsi: if i.count(name) == 1:
        B.append(i)

print('the following numbers match:')
for i in B:
    index_space = i.index(' ') + 1
    print(i[index_space:])
Sign up to request clarification or add additional context in comments.

1 Comment

works exactly as described, +1. it will me a good start. thanks.
0

If you want to get all the values for a name, you need to go through all the items in race:

def displayInfo(race, name):
    # go through the race list and return values for given name
    found = [x[1] for x in race if x[0] == name]
    if found:
        # return the values separated by comma
        return ", ".join(found)
    # no item found for the given name
    return "not found"

1 Comment

Thanks ++. this works well as per Original code.

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.