-3

I'm making a hangman game to get more familiar with Python, and I decided to implement reading text files to get death messages etc. Since I added this sub program, the program closes as soon as this sub program is called.

def life_loss_messages_create():
    p_life_loss_messages = []
    life_loss_messages_file = open("life_loss_messages.txt", "r")

    for message_line in life_loss_messages_file:
        message_line.strip()
        this_line = message_line.split("-")
        p_life_loss_messages.append(this_line)
    life_loss_messages_file.close()

    return p_life_loss_messages

Why will this not run past this sub program outside of IDLE? If opened and ran with IDLE it works fine, but not if double clicked and ran itself.

CharlRae
  • 1
  • 2
  • 2
    Most likely your script fails to open that `life_loss_messages.txt` file. See [this question](https://stackoverflow.com/questions/17658856/why-am-i-getting-a-filenotfounderror) for solutions. – Aran-Fey May 21 '22 at 17:46
  • 3
    That said, "Why doesn't my program work?" isn't exactly a suitable question for SO. It's your job to include all the necessary information in the question, and if you can't do that, well, it doesn't really belong here. We don't play guessing games here. – Aran-Fey May 21 '22 at 17:48
  • @Aran-Fey my question was not why does my program not work. My question was why does it close as soon as this function is called. – CharlRae May 21 '22 at 17:50
  • ...but the question title is "Why won't my Python program run outside of IDLE"? – Aran-Fey May 21 '22 at 17:51
  • @Aran-Fey It runs fine inside IDLE. Which is why I'm not really sure what the problem is. My question, you are correct, is why does it not run outside of IDLE. I'm not really sure what other "necessary information" I need to give. – CharlRae May 21 '22 at 17:54
  • All the information people need to reproduce your problem. That includes all the (relevant) code, all files your code wants to open, and the way you start the program. Without all of that, we can only guess why your code crashes. – Aran-Fey May 21 '22 at 17:57

2 Answers2

0

Given that it work on the IDLE but crashed when open it in others ways like double clicking the file, you can find the problem by opening your console, change directory to the folder of the file and run >python myscript.py whatever the problem is you will see the error there as the console will remain open unlike by double click which closed it immediately.

Copperfield
  • 8,131
  • 3
  • 23
  • 29
-1

Try putting whole directory there, not just filename.

dod0lp
  • 84
  • 5
  • Adding the whole directory still does the same thing. The program will run without issues or errors in IDLE but as soon as it's launched by itself outside of an environment it will close immediately after this sub program being called. Thanks for the suggestion. – CharlRae May 21 '22 at 18:05
  • You said something about "doubleclicking file" may I know which file ? – dod0lp May 21 '22 at 18:32