How to make an error message for this situation?

4
arq=open('filmes.txt','r')
filmes = arq.readlines()
def buscaPorGenero():

genero=str(input("Digite o gênero do filme: "))
while(True):
    for linha in filmes:
        linha = linha.split(";")
        if genero in str(linha[1]):
            print(linha[0])
    break

When the movie is not in the file, I wanted to show an error msg but I could not, I tried in several ways with while and for and I did not find one that worked. In my txt file the info is separated by ;

    
asked by anonymous 12.12.2017 / 03:06

3 answers

4

Create a Boolean control variable. I named it achou .

achou = False # inicializa a variável
while(True):
    for linha in filmes:
        linha = linha.split(";")
        if genero in str(linha[1]):
            achou = True # seta para verdadeiro se tem algum filme com esse gênero
            print(linha[0])
    break

if not achou: # se não encontrou, então...
    print("O gênero não foi encontrado :( ")

The initialization value is false, only true if it has gone through all the iterations without finding any movie of the genre to search.

In this example you gave, such as @AndersonCarlosWoss said, you do not need the loop while infinite with break soon when for is over. It looks like this:

achou = False # inicializa a variável
for linha in filmes:
    linha = linha.split(";")
    if genero in str(linha[1]):
        achou = True # seta para verdadeiro se tem algum filme com esse gênero
        print(linha[0])
    
12.12.2017 / 03:26
1

Since you are creating a function to search, it is not interesting that you make print . This generates side effects: it will not always be desired to execute the function and get the result in the terminal, so the ideal is to return the values instead of displaying them. We call these pure functions functions if you want to study more about it.

Incidentally, reading the value inside the function also breaks with the atomicity of the function; and if it is desired to seek the films of a specific genre? Do I have to create another function for this? Ideally this should be a function parameter.

Another change I took freely to do was to change the way the file is analyzed. You are separating the values of the columns in the text file with a semicolon character, ; , this clearly characterizes the CSV format, so you do not have to read the file as such. So your file could be something like, where the first line will be the column names:

filmes.csv

name;genre;id;year
O Hobbit;fantasia;343433434;2007

And in Python, we can define a generator that will look for the genre in the file. The parameters will be the genre that we want to search and the file where it will be searched. For ease, we can set this second parameter to a default value. Then we will open the file for reading, define a CSV reader and go through the lines, verifying if the film belongs to the genre you want; if yes, it will be returned via yield , which is basically the return of a generator.

import csv

def search_by_genre(genre, filename='filmes.csv'):
    """ Busca em um arquivo todos os filmes de um determinado gênero.

    Atributos:
        genre (string): Nome do gênero a ser buscado.
        filename (string): Nome do arquivo onde os filmes estão armazenados.

    Retorno:
        Retorna um gerador (iterável) com todos os filmes referentes ao gênero.

    Exceções:
        FileNotFoundError: Quando o arquivo indicado por 'filename' não existir.
    """
    with open(filename, 'r') as stream:
        reader = csv.DictReader(stream, delimiter=';')
        for row in reader:
            if row['genre'].lower() == genre.lower():
                yield row
  

I used the lower() method to check the genre to make the search insensitive to the case, causing the search by Fantasia to return the same results as fantasia , for example.

To display all the movies of a genre read from the user, as it is placed in the question, we can do:

if __name__ == '__main__':
    genre = input('Digite o gênero do filme: ')
    movies = search_by_genre(genre, filename='filmes.csv')
    for movie in movies:
        print(movie['name'])

See working at Repl.it

So, by running the program, we would have:

Digite o gênero do filme: fantasia
O Hobbit
    
12.12.2017 / 12:47
0

Make an IF-ELSE. If the value returned from IF is false, program execution skips to the code block within ELSE, hence within this block of code you write the message display code. For example:

if genero in str(linha[1]):
            print(linha[0])
else:
            print("Erro!")

I do not know if ELSE writes like this in this language, I'm sorry. But that's the logic.

    
12.12.2017 / 03:27