Python Hangman根本不工作

时间:2015-10-20 17:08:47

标签: python

我所有的功能都是分开工作的,当我把它们放在一起时,它会断裂,永远不会停止做任何事情而且我只是太累了,你介意帮忙吗?请

我目前遇到的错误是:

分配前引用的局部变量 它要求每次做出两次决定 图纸无法正常工作。

__author__ = 'Rodrigo Cano'
#Modulos

import random
import re

#Variables Globales

intentos = 0
incorrectos = 0
palabras = [(1,"disclosure"),(1,"thenational"),(1,"foals"),(1,"skepta"),(1,"shamir"),(1,"kanye"),
              (1,"fidlar"),(1,"lcdsoundsystem"),(1,"lorde"),(1,"fkatwigs"),(1,"miguel"),(1,"rtj"),
              (1,"boniver"),(1,"strokes"),(2,"vaporwave"),(2,"witchouse"),(2,"shoegaze"),(2,"memerap"),
              (2,"paulblartisoursaviour"),(3,"glockenspiel"),(3,"aesthetic"),(3,"schadenfreude"),
              (3,"bonvivant"),(3,"swag"),(3,"jue")]

palabra_a_jugar = ''
Array_Palabra = []
Nuevas_Letras = ''
letras = []
Vidas = 0
i = len(Array_Palabra)


#Funciones

def Definir_Palabra():
    eleccion = int(input("Bienvenido, que categoria quiere usar:"
                     '\n'"1 - Musica que Escuche Mientras Lo Hacia"
                     '\n'"2 - Generos Musicales"
                     '\n'"3 - Palabras Pretenciosas"))
    palabras_escogidas = [i for i in palabras if eleccion in i ]
    palabra_a_jugar = str(palabras_escogidas[random.randint(0,len(palabras_escogidas))].__getitem__(1))
    Array_Palabra = len(palabra_a_jugar) * ['*']
    return palabra_a_jugar, Array_Palabra

def Letras_En_Palabra(letra):
    letras = [i for i, x in enumerate(palabra_a_jugar) if x == letra]
    for i in range (0, len(letras)):
        Array_Palabra[letras] = letra
    return letras,Array_Palabra

def Letras_Jugadas(letra):
    for i in range(0,len(Nuevas_Letras)):
        Nuevas_Letras = re.findall(letra,Nuevas_Letras[i])
        if Nuevas_Letras != []:
            return 1
    return Nuevas_Letras

def Eleccion():
    Choice = input("Quiere Jugar?")
    if Choice == 'si':
        Choice = 1
    elif Choice == 'no':
        Choice = 0
    return Choice

def hangman_graphic(vidas):
    if vidas == 0:
            print ("              ")
            print ("              ")
            print ("              ")
            print ("              ")
            print ("              ")
            print ("              ")
    elif vidas == 1:
            print ("________      ")
            print ("|      |      ")
            print ("|      0      ")
            print ("|             ")
            print ("|             ")
            print ("|             ")
    elif vidas == 2:
            print ("________      ")
            print ("|      |      ")
            print ("|      0      ")
            print ("|             ")
            print ("|             ")
            print ("|             ")
    elif vidas == 3:
            print ("________      ")
            print ("|      |      ")
            print ("|      0      ")
            print ("|     /       ")
            print ("|             ")
            print ("|             ")
    elif vidas == 4:
            print ("________      ")
            print ("|      |      ")
            print ("|      0      ")
            print ("|     /|      ")
            print ("|             ")
            print ("|             ")
    elif vidas == 5:
            print ("________      ")
            print ("|      |      ")
            print ("|      0      ")
            print ("|     /|\     ")
            print ("|             ")
            print ("|             ")
    elif vidas == 6:
        print ("________      ")
        print ("|      |      ")
            print ("|      0      ")
            print ("|     /|\     ")
            print ("|     /       ")
            print ("|             ")
    else:
            print( "________      ")
            print ("|      |      ")
            print ("|      0      ")
            print ("|     /|\     ")
            print ("|     / \     ")
            print ("womp womp")
# Juego
Choice = Eleccion()
def Juego(Choice):
    vidas = 0
    i = len(Array_Palabra)
    while Choice == 1:
        print(Definir_Palabra())
        while i != 0 :
            tiro = str.lower(input("adivine una letra"))
            if Letras_Jugadas(tiro) != 1:
                Nuevas_Letras = Nuevas_Letras + tiro
                letras = Letras_En_Palabra(tiro)
                if Letras_Jugadas(tiro) != []:
                    i = len(letras) - 1
                    print("Letras Utilizadas",Nuevas_Letras)
                    print(Letras_En_Palabra(tiro))
                    print(hangman_graphic(vidas))

                else:
                    Vidas = Vidas + 1
                    print(hangman_graphic(vidas))
                    print("WROOOONG")
                    print("Letras Utilizadas",Nuevas_Letras)
                if Vidas == 7:
                    i = 0
            else:
                print("Letra ya Juagada",Nuevas_Letras)
Choice = Eleccion()

Juego(Choice)

1 个答案:

答案 0 :(得分:1)

好吧,你被问到是否要玩两次的原因是你做的

# Juego
Choice = Eleccion()
def Juego(Choice):
    ...
Choice = Eleccion()

因此,当您运行代码时,它会下降,询问您是否要播放一次,然后在继续之前询问您是否要再次播放。

从那里,我发现了一些错误:

首先,在函数Juego中创建变量vidas并将其值设置为0,然后尝试将Vidas递增1。 在python中,变量区分大小写,因此Vidasvidas是两个不同的东西。

其次,您可能希望检查您的变量和函数命名方案,因为它们不一致。通常,python使用小写变量,必要时用下划线分隔。例如。 function_namevariable_name

您的代码中可能还有其他错误,但这些错误对我来说非常明显。