1

I´m making a searching tool with Python for a csv file. In the last lines of code I make a double loop:

for letterText in Text:
    for letterBook in i:
        if letterText == letterBook:
            count = count + 1
        if count >= countLetter/2:
            print(i.upper(),":",bookList[i].upper())
            break

But the problem is that I want to print (i.upper(),":",bookList[i].upper()) only once peri . I can´t figure out what I´m doing wrong. Thank you for your help. Here is the full code.

import csv
Text = input("Book...")
data = open(r"C:\Users\file.csv")
dataReader = csv.reader(data, delimiter = ";")
count = 0
countLetter = 0
bookList= dict()
letterList= list()

for letterText in Text:
    letterList.append(letterText)
    countLetter = countLetter + 1

for row in dataReader:
    bookList[row[0]]= row[1]

for (i) in bookList:
    for letterBook in i:
        LetterBookList = list(letterBook)
    count = 0
    #exact book name typed
    if Text == i:
        print(i.upper(),":",bookList[i].upper())
    for letterText in Text:
        for letterBook in i:
            if letterText == letterBook:
                count = count + 1
            if count >= countLetter/2:
                print(i.upper(),":",bookList[i].upper())
                break
4
  • How does the input("Book...") look like? Commented Jan 21, 2020 at 15:10
  • 1
    Welcome to Stack Overflow! Check out the tour and How to Ask. This would be easier to answer if you provided a minimal reproducible example. At the moment your example isn't reproducible since it's missing the user input, CSV, output, and desired output. Though it might be clearer if you get rid of the input and CSV and just put them statically in the code. Commented Jan 21, 2020 at 15:16
  • Are u trying to see if any typed text matches a part of the book title in any way. as in inputting "is" will give us all the book titles containing "is" ? Commented Jan 21, 2020 at 15:21
  • @SamuelMiddendorp I'm trying to get printed all the book that have at least half or their words equal to the input. Commented Jan 21, 2020 at 15:29

3 Answers 3

0

I think your issue is that break works for inner loop only (for letterBook in i:). You need to change your code to also break from the outer loop (for letterText in Text:) and put it into an else branch of if Text == i:. Your code seems to check for partial text match. For that I would recommend using some other techniques like builtin difflib

Sign up to request clarification or add additional context in comments.

Comments

0

There aren't many info, but you can use a set to keep track of what you printed and check on it, here's a modified code

printed = set()
for letterText in Text:
    for letterBook in i:
        if letterText == letterBook:
            count = count + 1
        if count >= countLetter/2 and i not in set:
            print(i.upper(),":",bookList[i].upper())
            printed.add(i)
            break

Comments

0

The issue with your code is that you have two for loops and only one break statement.

Once count >= countLetter/2 is true, i.upper(),":",bookList[i].upper() is printed and then the break statement is called. The break statement breaks out of the for letterBook in i loop but it does not break out of the for letterText in Text loop and therefore this loop goes onto its next iteration and so on.

I often use flag variables to break out of such loops. A flag variable like a boolean variable could be false initially but will store true once count >= countLetter/2 is true and if the flag variable is true, another break statement is executed in the for letterText in Text loop.

Check out the code below:

import csv
Text = input("Book...")
data = open(r"C:\Users\file.csv")
dataReader = csv.reader(data, delimiter = ";")
count = 0
countLetter = 0
bookList= dict()
letterList= list()

for letterText in Text:
    letterList.append(letterText)
    countLetter = countLetter + 1

for row in dataReader:
    bookList[row[0]]= row[1]

for (i) in bookList:
    for letterBook in i:
        LetterBookList = list(letterBook)
    count = 0
    #exact book name typed
    halfThresholdReached = False
    if Text == i:
        print(i.upper(),":",bookList[i].upper())
        continue

    for letterText in Text:
        for letterBook in i:
            if letterText == letterBook:
                count = count + 1
            if count >= countLetter/2:
                print(i.upper(),":",bookList[i].upper())
                halfThresholdReached = True
                break
        if halfThresholdReached:
            break

EDIT: There is another piece that I forgot to look at. You are also checking if if Text == i in the for (i) in bookList loop and if this is true, then you must continue on to the next iteration of the same loop and not check the rest of the Text. So if it returns true, then add a continue statement to proceed to the next iteration.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.