Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

two_games_in_one

This code is made up of two games (Riddle game and Hangman). I am still working to improve this code, but I am currently having trouble with one part. When I run this code it only runs the first game (riddle game) and it exits the program after it asks you "Do you want to play Hangman".

import os 
import sys
import random
# get the list of people on prodigy line 

answer1 = "Candle".lower()
answer2 = "Fire".lower() 
answer3 = "Short".lower()
answer4 = "Stamp".lower()
answer5 = "Clock".lower()
wrong_anw = []
good_anw = []
things = [
    "Flower".lower(),
    "Rocket".lower(),
    "Basketball".lower(), 
    "Book".lower(),
    "Helicopter".lower(),
    "Submarine".lower(),
    "Tennis".lower(),
    "Monopoly".lower(),
    "Television".lower(),
    "Computer".lower(),
    "Skullkid".lower(),
    "Backpack".lower()
]
def clear():
    if os.name == "nt":
        os.system("cls")
    else:
        os.system("clear")
def welcome():
    print("Welcome to the riddle game!")
    print(" ")
    print("You most get 3 correct in order to unlock the secret game.")

def first_game():   
    question1 = input("1.I’m tall when I’m young and I’m short when I’m old. What am I? ").lower()
    if question1 == answer1:
        good_anw.append(answer1)
        print("Great job!. You have a total of {}/5 currect.".format(len(good_anw))) 
    else:
        print("Wrong answer!")

    question2 = input("2.If I drink, I die. If i eat, I am fine. What am I? ")
    if question2 == answer2:
        good_anw.append(answer2)
        print("Awsome! You have a total of {}/5 currect.".format(len(good_anw))) 
    else:
        print("Wrong answer!")
    question3= input("3.What word becomes shorter when you add two letters to it? ")
    if question3 == answer3:
        good_anw.append(answer3)
        print("Great job! You have a total of {}/5 currect.".format(len(good_anw)))
    else:
        print("Wrong answer")

    question4 = input("4.What travels around the world but stays in one spot? ")
    if question4 == answer4:
        good_anw.append(answer4)
        print("Great!  You have a total of {}/5 currect.".format(len(good_anw))) 
    else:
        print("Wrong answer!")
    question5 = input("5.What has hands but can not clap?")
    if question5 == answer5:
        good_anw.append(answer5)
        print("Alright! Great job. You have a total of {}/5 currect.".format(len(good_anw)))
    else:
        print("Wrong answer!")

        print(" ")

first_game()
def unlock(): 
    clear()
    if len(good_anw) >= 3:
        print("You unlocked the Hangman game")
        start_game = input("Do you want to play Hangman Y/n? ")
        if start_game != 'n':
            print("Lets continue.")
        else:
            print("Goodbye")
            sys.exit                
    else:
        print("Game over you lose!")

unlock()
def draw(bad_guesses, good_guesses,secret_word):

    print("Strikes {}/10".format(bad_guess))
    print(" ")

    for letter in secret_word:
        if letter in good_guesses:
            print(letter, end= " ")
        else:
            print('_', end = " ")
    for letter in bad_guesses:
        print(letter, end =' ')
        print('\n\n')

    print(' ')


def hangman(good_guesses, bad_guesses):

    good_guesses=[]
    bad_guesses=[]
    secret_guess = random.choice(members)
    while True:
        s
        guess = input("Guess a letter: ").lower()
        if len(guess) != 1:
            print("You can only guess one letter")
        elif not guess.isalpha():
            print("Your guess most be a letter")
        elif guess in good_guesses or guess in bad_guesses:
            print("You have already guessed this letter")
        else:
            return guess

def play(done):
    clear()
    bad_guesses= [] 
    good_guesses = []
    secret_word = random.choice(members)

    while True:
        draw(bad_guesses, good_guesses, secret_word)
        guess = hangman(bad_guesses, good_guesses)

        if guess in secret_word:
            good_guesses.append(guess)
            found = True
            for letter in secret_word:
                if letter not in good_guesses:
                    found = False
            if found:
                print("You win!")
                print("The secret word was {}.".format(secret_word))
                done = True 
        else:
            bad_guesses.append(guess)
            if len(bad_guesses) == 10:
                draw(bad_guesses, good_guesses, secret_word) 
                print("You lose better luck next time")
                print("The secret word was {}.".format(secret_word))
                done = True

        if done:
            play_again = input("Would you like to play again? Y/n").lower()
            if play_again != "n":
                return play(done = False)
            else:
                sys.exit()

1 Answer

Steven Parker
Steven Parker
243,201 Points

It looks like the "play" function is intended to start the hangman game, but it is not called by the main program.

Suggestion: put all the main program lines after the function definitions to make it easier to see what the program does.