Home > other >  Code not running as it intended after calling the very 1st function
Code not running as it intended after calling the very 1st function

Time:06-28

hope you guys can help me I get this source code from the hangman game then added an additional content genre and difficulty

after running the 1st round and if the player decided to play again which will trigger the loop function and call again the words function when ask what genre the player wants after input it stops there and doesn't continue initialization and hangman function

forgive me if I have a wrong grammar

import random
import time

# Initial Steps to invite in the game:
print("\nWelcome to Hangman game by IT SOURCECODE\n")
name = input("Enter your name: ")
print("Hello "   name   "! Best of Luck!")
time.sleep(2)
print("The game is about to start!\n Let's play Hangman!")
time.sleep(3)

def words():
    global word
    mode = int(input("What difficulty do you want to play? \n1-Normal \n2-Hard\n"))
    a = random.randint(0,9)
    if mode == 1:
        genre = int(input("Select a Genre you want to play \n1-Health \n2-Country \n3-Food \n4-Animal \n5-Items\n"))
        if genre == 1:
            word_to_guess = ["oxygen", "energy", "nutrients", "carbohydrates", "proteins", "pharmacist ", "vitamins", "calories", "nutrition", "digestion"]
            word_hint = ["Colorless, odorless and tasteless gas. It will support life", "Is the ability to do work.", "Substances found in food that help your body grow and develop.", "Also known as carbs.", "Substance that consists of amino acid residues joined by peptide bond.", 
            "A person who is professionally qualified to prepare and dispense medicinal drugs.", "A group of substances that are needed for normal cell function", "Energy released when your body digests and absorbs food.", "Substances found in food that help your body grow and develop.", "The process of breaking down food."]
            word = word_to_guess[a]
            hint = word_hint[a]
            print("here is your hint", hint)  
        elif genre == 2:
            word_to_guess = ["china", "united_kingdom", "italy", "united_states", "canada", "egypt", "indonesia", "poland", "france", "ukraine"]
            word_hint = ["It is the world's most populous country.", "Consists of a group of islands off the northwest coast of europe.", "A european country with a long mediterranean coastline.", "The world's third largest country in size.", "It is the second largest country in the world.",
            "country located in the northeastern corner of africa.", "The world's fourth-most populous country.", "located in the very centre of europe.", "The largest country in western europe.", "Currently in war with Russia."]
            word = word_to_guess[a]
            hint = word_hint[a]
            print("here is your hint", hint)  
        elif genre == 3:
            word_to_guess = ["adobo", "bulalo", "sinigang", "maja_blanca", "pastillas", "polvoron", "yema", "halo_halo", "taho", "leche_flan"]
            word_hint = ["Meat marinated in vinegar and soy sauce.", "Cooking beef shanks and bone marrow.", "Translation to the English word, “stew”.", "Filipino dessert with a gelatin.", "Also known as pastiyema milk-based confection.", "Filipino shortbread cookie.", "A sweet custard confectionery from the philippines.",
            "Most love dessert in philippines especially in summer.", "Man with yelling in the morning selling a fresh soft/silken tofu.", "A dessert of sweetened egg custard with a caramel topping."]
            word = word_to_guess[a]
            hint = word_hint[a]
            print("here is your hint", hint)      
        elif genre == 4:
            word_to_guess = ["dog", "philippine_eagle", "cow", "carabaw", "giraffe", "cheetah", "lion", "antarctic_blue_whale", "sloths", "elephant "]
            word_hint = ["Man's best friend", "National bird of the Philippines.", "Most common animal who produces milk.", "National animal in the Philippines.", "Longest neck in the animals.", "Faster animal in land.", "King of the jungle.", "Largest animal in water.", "Slowest and laziest in animal.", "Strongest animal in Africa."]
            word = word_to_guess[a]
            hint = word_hint[a]
            print("here is your hint", hint)  
        elif genre == 5:
            word_to_guess = ["camera", "pen", "book", "newspaper", "bottle", "key", "bag", "shoe", "umbrella", "bicycle"]
            word_hint = ["A device for recording visual images in form of photographs and film.", "An instrument for writing or drawing with ink.", "An instrument for writing or drawing with ink.", "A printed publication containing news.", "A container made of glass or plastic, used for storing drinks.", "A small piece of shaped metal, used to open a lock.",
            "Used for carrying things.", "It is a footwear covering the foot.", "Used as protection against rain and sun.", "A vehicle with two wheels and propelled by pedals."]
            word = word_to_guess[a]
            hint = word_hint[a]
            print("here is your hint", hint)  
        else:
            print("Invalid input, try again")
            words()

    elif mode == 2:
        genre = int(input("Select a Genre you want to play \n1-Country's Capital \n2-Food \n3-Health \n4-Movies \n5-Fast-food chain\n"))
        if genre == 1:
            word_to_guess = ["brussels", "brasilia", "athens", "ottawa", "pyongyang", "moscow", "warsaw", "islamabad", "helsinki", "canberra"]
            word = word_to_guess[a]
        elif genre == 2:
            word_to_guess = ["menudo", "pakbet", "samgy", "lechon_kawali", "caldereta", "sipo_egg", "shabu-shabu", "starbucks", "pares-mami","tollhouse"]
            word = word_to_guess[a]
        elif genre == 3:
            word_to_guess = ["micronutrient", "rejuvenate", "delicate", "restorative", "sanitary", "condition", "recovery", "therapy", "nutrition", "medicare"]
            word = word_to_guess[a]
        elif genre == 4:
            word_to_guess = ["inception", "whiplash", "interstellar", "atonement", "schindler_list", "bliss", "unforgettable", "ordinary people", "maledicto", "heneral luna"]
            word = word_to_guess[a]
        elif genre == 5:
            word_to_guess = ["taco_bell", "burger_king", "wendys", "krispy_kreme", "subway", "dominos_pizza", "pizza_hut", "starbuck", "dairy_queen", "popeyes"]
            word = word_to_guess[a]
        else:
            print("Invalid input, try again")
            words()
    else:
        print("Invalid input, try again")
        words()


# The parameters we require to execute the game:
def initialization():
    global count
    global display
    global already_guessed
    global length
    global play_game
    global word
    length = len(word)
    count = 0
    display = '_' * length
    already_guessed = []
    play_game = ""
    print("initialization")

# Initializing all the conditions required for the game:
def hangman():
    global count
    global display
    global word
    global already_guessed
    global play_game
    limit = 5
    guess = input("This is the Hangman Word: "   display   " Enter your guess: \n")
    guess = guess.strip()
    if len(guess.strip()) == 0 or len(guess.strip()) >= 2 or guess <= "9":
        print("Invalid Input, Try a letter\n")
        hangman()


    elif guess in word:
        already_guessed.extend([guess])
        index = word.find(guess)
        word = word[:index]   "_"   word[index   1:]
        display = display[:index]   guess   display[index   1:]
        print(display   "\n")

    elif guess in already_guessed:
        print("Try another letter.\n")

    else:
        count  = 1

        if count == 1:
            time.sleep(1)
            print("   _____ \n"
                  "  |      \n"
                  "  |      \n"
                  "  |      \n"
                  "  |      \n"
                  "  |      \n"
                  "  |      \n"
                  "__|__\n")
            print("Wrong guess. "   str(limit - count)   " guesses remaining\n")

        elif count == 2:
            time.sleep(1)
            print("   _____ \n"
                  "  |     | \n"
                  "  |     |\n"
                  "  |      \n"
                  "  |      \n"
                  "  |      \n"
                  "  |      \n"
                  "__|__\n")
            print("Wrong guess. "   str(limit - count)   " guesses remaining\n")

        elif count == 3:
           time.sleep(1)
           print("   _____ \n"
                 "  |     | \n"
                 "  |     |\n"
                 "  |     | \n"
                 "  |      \n"
                 "  |      \n"
                 "  |      \n"
                 "__|__\n")
           print("Wrong guess. "   str(limit - count)   " guesses remaining\n")

        elif count == 4:
            time.sleep(1)
            print("   _____ \n"
                  "  |     | \n"
                  "  |     |\n"
                  "  |     | \n"
                  "  |     O \n"
                  "  |      \n"
                  "  |      \n"
                  "__|__\n")
            print("Wrong guess. "   str(limit - count)   " last guess remaining\n")

        elif count == 5:
            time.sleep(1)
            print("   _____ \n"
                  "  |     | \n"
                  "  |     |\n"
                  "  |     | \n"
                  "  |     O \n"
                  "  |    /|\ \n"
                  "  |    / \ \n"
                  "__|__\n")
            print("Wrong guess. You are hanged!!!\n")
            print("The word was:",already_guessed,word)
            play_loop()

    if word == '_' * length:
        print("Congrats! You have guessed the word correctly!")
        play_loop()

    elif count != limit:
        hangman()

# A loop to re-execute the game when the first round ends:

def play_loop():
    global play_game
    play_game = input("Do You want to play again? y = yes, n = no \n")
    while play_game not in ["y", "n","Y","N"]:
        play_game = input("Do You want to play again? y = yes, n = no \n")
    if play_game == "y":
        words()
    elif play_game == "n":
        print("Thanks For Playing! We expect you back again!")
        exit()

words()
initialization()
hangman()

CodePudding user response:

It looks like you should be calling hangman() right after you call words() in the playloop() method.

  • Related