Python Hangman无法正常工作

时间:2015-10-16 17:02:12

标签: python

所以,我已经为一个编码类完成了我的Hangman项目,并且它停留在一个循环中,它只是一直在询问你是否想玩,之后它定义了要播放的单词但它也告诉你要播放哪个单词你要去玩。我认为链式的ifs和ifs有一些错误的变量。

所以,求求你的帮助。

__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")]
Ascii_Art =[""]
Dibujo = ' '
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

# Juego
Choice = Eleccion()
def Juego(Choice):
    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))
                else:
                    Vidas = Vidas + 1
                    Dibujo += Ascii_Art[Vidas]
                    print("WROOOONG")
                    print(Dibujo)
                    print("Letras Utilizadas",Nuevas_Letras)
                if Vidas ==9:
                    i = 0
            else:
                print("Letra ya Juagada",Nuevas_Letras)
        Eleccion()

Juego(Choice)

2 个答案:

答案 0 :(得分:0)

Juego中的最后一行应为Choice = Eleccion(),否则,您实际上并未更改Choice的值,因此循环会无限期地继续。

def Juego(Choice):
    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))
                else:
                    Vidas = Vidas + 1
                    Dibujo += Ascii_Art[Vidas]
                    print("WROOOONG")
                    print(Dibujo)
                    print("Letras Utilizadas",Nuevas_Letras)
                if Vidas ==9:
                    i = 0
            else:
                print("Letra ya Juagada",Nuevas_Letras)
        """
        >>>> CHANGED <<<<
        """
        Choice = Eleccion()   

否则,您使用它的方式只是调用函数Eleccion,但不会将值返回到Choice函数范围内的Juego变量

答案 1 :(得分:0)

只需要缩进:)

import random

# defining the main method
def main():
print "Hello Welcome to Python Hangman!!!"
random_word()
user_input()
# defining the dictionary_list to be used as the guessing word
def random_word():
dictionary_list= ["ant", "bath", "car", "dog", "electric", "foil", "grape", "heat", "ice", "jolly",
                   "knight", "lemon", "money", "need", "open", "park", "quote", "rough", "salty",
                   "teach", "under", "village", "warmth", "xavier", "yelp", "zipper"]


# making rand global so it can be used within deifferent functions
global rand
# randomly choosing a word from the dictionary_list and assign to the variable rand
rand = random.choice(dictionary_list)
return rand

# user guessing
def user_input():
guesses = 0
wrong_guess = 0
guess_left = 10
correct_letters_list = ""
wrong_letters_list =""
print "You have \033[1;34;15m 10 \033[0;30;15m guesses"

#getting the length of the word
length = len(rand)
print "The word is: " + "\033[1;35;15m" + str(length) + "\033[0;30;15m" + " characters long"

# loops around until the user has used all their 6 guesses are used up
while guesses < 10 :
    guess = raw_input("Guess a Letter [a-z]: ").lower()
    # stop case sensitivity
    # if the letter guessed by the user is in rand string print correct else print wrong
    if guess in rand:
        correct_letters_list += guess
        print "\033[1;32;15mcorrect"
        print "correct letters:"
        print "\033[0;30;15m" + correct_letters_list
        print "incorrect letters:"
        print "\033[0;30;15m" + wrong_letters_list
        # each time the user guesses + 1 to the guesses tally
        guesses +=1
        guess_cal = guess_left - guesses
        print "You have: " + str(guess_cal) + " guesses left"
        # if rand contains guess more than once tell user
        if rand.count(guess) > 1 :
            print "The word contains the letter " + str(guess) + " " +  "\033[1;36;15m" + str(rand.count(guess)) + "\033[0;30;15m" + " times"
    else:
     wrong_letters_list += guess
     print "\033[1;31;15mwrong"
     print "incorrect letters:"
     print "\033[0;30;15m" + wrong_letters_list
     print "correct letters:"
     print "\033[0;30;15m" + correct_letters_list
     guesses += 1
     guess_cal = guess_left - guesses
     print "You have: " + str(guess_cal)  + " guesses left"
     wrong_guess += 1
     if wrong_guess == 1:
         print "________      "
         print "|      |      "
         print "|             "
         print "|             "
         print "|             "
         print "|             "
     if wrong_guess == 2:
         print "________      "
         print "|      |      "
         print "|      0      "
         print "|             "
         print "|             "
         print "|             "
    if wrong_guess == 3:
        print "________      "
        print "|      |      "
        print "|      0      "
        print "|     /       "
        print "|             "
        print "|             "
    if wrong_guess == 4:
        print "________      "
        print "|      |      "
        print "|      0      "
        print "|     /|      "
        print "|             "
        print "|             "
    if wrong_guess == 5:
        print "________      "
        print "|      |      "
        print "|      0      "
        print "|     /|\     "
        print "|             "
        print "|             "

    if wrong_guess == 6:
        print "________      "
        print "|      |      "
        print "|      0      "
        print "|     /|\     "
        print "|     /       "
        print "|             "

    if wrong_guess >= 7:
        print "________      "
        print "|      |      "
        print "|      0      "
        print "|     /|\     "
        print "|     / \     "
        print "|             "
    choice = raw_input("Do you wish to guess the word? (Y/N): ").lower()
    if choice == 'y':
        word = raw_input("Please enter the word: ").lower()
        if word == rand:
            print "Well Done you Guessed Correctly, the word was: " + rand
            # if the user has guessed correctly + 10 to the guesses tally to end the game
            guesses += 10
        else:
            print "Incorrect, Game Over!! The word was: " + rand
            guesses += 10
    # if user chooses n continue with the game
    if choice == 'n':
        continue

# if the user has used up all their 10 guesses print Game Over!!
if guesses == 10:
    print "End of Game you ran out of guesses!!!"

if __name__ == "__main__":
main()