-2

Im testing new things and I landed on python... As first code i found the classic hangman game.

I followed a tutorial that shows how to do it step by step but when it comes to wordlist I've got lost... So far I've been retrieving the words from a variable "_WORDS" which it has some strings as properties. I have now a "words.txt" file from which I want to get the words instead of the variable _WORDS = ("word1", "word2", "etc"). I found some examples but I cant hack it... some inspiration? This is my code...

_WORDS = open("words.txt", 'r')

_IFYOUWIN = ("you won!")

def __init__(self):
    self._word = choice(self._WORDS)
    self._so_far = "-" * len(self._word)
    self._used = []
    self._wrong_answers = 0

def play(self):
    self._reset_game()
    self._start_game()
    while self._wrong_answers < len(self._HANGMAN) and self._so_far != self._word:
        self._print_current_progress()
        guess = self._user_guess()
        self._check_answer(guess)

I tried importing "os" like this:

import os
_WORDS = os.path.expanduser("~/Desktop/pythontlearn/words.txt")

but no luck.

Bill P
  • 3,622
  • 10
  • 20
  • 32
Fernando Löpez
  • 61
  • 2
  • 11
  • 1
    Possible duplicate of [How to read a file line-by-line into a list?](https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list) – wjandrea Oct 01 '19 at 14:56
  • If it's not a duplicate, you need to [edit] the question to include a [mre] of the problem. Please read [ask]. – wjandrea Oct 01 '19 at 14:59
  • Is not a duplicated, I navigated through the answers in stack overflow but no luck. Im gonna keep looking. It does feel very wrong to have people downvoting questions but not giving answers... stackoverflow has become really unfriendly to people who want to learn new things. – Fernando Löpez Oct 02 '19 at 13:16
  • You're getting downvotes cause your question is unclear, you haven't shown any effort at researching this yourself, and your code is incomplete and confusing. Again, please read [ask] and [mre]. – wjandrea Oct 02 '19 at 13:24
  • ok, thanks, i'll keep it mind to next questions. – Fernando Löpez Oct 02 '19 at 16:42

1 Answers1

0

for the record I found this working for me

path = '/words.txt'
_WORDS = open(path,'r')

but Im still not sure why it does not work with something like

_WORDS = open("words.txt", 'r')

they look exactley the same to me

Fernando Löpez
  • 61
  • 2
  • 11