Hello, I have a problem with the hangman game in Python

0

Well I have the game and I do not know how to do that in the variables ( tupalabra and palabra ) I choose the list that I created from random words in order to start playing. Because I have them empty and it shows me that I have already won directly and I can not find a way ...
That's what I have and I do not know how to face it ..

import time
nombre=input("¿Como te llamas? ")
print(" ")
print("hola, "+nombre," Es hora de jugar al ahorcado pequeño bolon")
print (" ") 
print("hola, ",    nombre, " Es hora de jugar al ahorcado pequeño bolon")
print("\n", "\n", "\n", "\n")


time.sleep(1)
print("Comienza a adivinar ")
time.sleep(0.5)
tupalabra=''
palabra=''
vidas=5

import random

with open('archisofi.txt', 'r') as d:
    lineas = [linea.split() for linea in d]

for linea in lineas:
    print(linea)

print(random.choice(linea))



while vidas > 0:

    fallas=0
    for letra in palabra:
        if letra in tupalabra:
            print(letra,end="")
        else:
            print("*",end="")
            fallas+=1
    if fallas==0:
        print("")
        print("Felicidadades ganaste, buen trabajo")
        break

    tuletra=input("Introduce una letra: ")
    tupalabra+=tuletra

    if tuletra not in palabra:
         vidas-=1
         print("Equivocacion")
         print("Tu tienes ",+vidas," vidas")
    if vidas == 0:
         print("Perdiste!")

else:
    print("Gracias por participar")
    
asked by Sofian Chani 29.05.2018 в 20:16
source

1 answer

1

If each line of the file has several words, and the file has several lines, you need to read everything in a single list in which each element is a word. To read it in a list in which each element is another list, as you do it, complicates the problem unnecessarily.

A simple way to do what I say is to read the entire file in a single string and break it into pieces by the "blanks" (which are not only the blank spaces, but also the carriage returns, tabs, etc. ) Like this:

with open('archisofi.txt', 'r') as d:
    lista_palabras = d.read().split()

Once all the words are in a "flat" list (as opposed to "nested"), the choice of a word is simple:

palabra = random.choice(lista_palabras)
    
answered by 29.05.2018 / 21:13
source