Function code works in IDLE, but the program hangs

0

I'm a beginner, free time training about two months ago and so far my programs had 5 lines, very simple. This program taught me a lot of new concepts, and it's part of a larger program that I plan to do.

def dicionario(palavra):
    f = open("portugues.txt", "r")
    for line in f:
        if palavra in line:
            f.close()
            return "Existe"
    f.close()
    return "Nao existe"

if __name__ == "__main__":
    palavra = input('Digite a palavra a procurar: ')
    dicionario(palavra)
input()

When I use IDLE, I open the file as f, and the for block works, returning "Exist" as long as the word is in the dictionary. When I execute the whole program, by Pycharm or Python itself, the program asks for the word to fetch and then it stops, without error message.    NOTE: The dictionary (portugues.txt) is in PYTHONPATH, in the same directory as the program. I use Windows 8.    What is missing? I would like to use this function in another program via import, can you do it like this?

    
asked by anonymous 14.07.2015 / 14:50

1 answer

1

Problem solved. The mistake was mine. I hoped that when I rolled the program by myself it would give me the answer on the screen. return only returns a value, but it does not become output on the screen. For this, the code would be:

def dicionario(palavra):
    f = open("portugues.txt", "r")
    for line in f:
        if palavra in line:
            f.close()
            return "Existe"
    f.close()
    return "Nao existe"

if __name__ == "__main__":
    palavra = input('Digite a palavra a procurar: ')
    print(dicionario(palavra))
input()

In this case, with print () involving the function, it does exactly what I wanted, which was to return on the screen whether the word exists or not when I run the module to test.

    
20.07.2015 / 15:25