1

I'm building a game of Hangman as my first python project. The game is currently being run in the terminal as I build it. I'm not using PyGame. Here's more of the relevant code.

five_letters = ['birds', 'hands', 'trees']
difficulty = 'easy'

def get_word(foo):
"""Based on difficulty generate a game_word, then turn it into a list to be returned"""
    if foo == 'easy':
        foo = random.choice(five_letters)
        foo = list(foo)

    if foo == 'medium':
        foo = random.choice(ten_letters)
        foo = list(foo)

    if foo == 'hard':
        foo = random.choice(fifteen_letters)
        foo = list(foo)

    return foo

game_word = get_word(difficulty)

I want to take the list and do the following:

  • Check if the player's guess is in the list ( I know I can iterate over a string).
  • If the guess is in the string place it in the appropriate space in the output.

I'm returning the string as a list so that I can find the value of a correct guess within the list to place it in the correct spot in the output.

For instance

game_word = 'birds'
player_guess = 's'

I want to output

_ _ _ _ s

But maybe I'm going about this all wrong. I just felt like I could shorten the part of the function that selected a random string and then turned it into a list.

4
  • 1
    foo = list(random.choice(a_list))? Commented May 17, 2017 at 20:54
  • 4
    random.sample(a_list, 1) would be my choice (it returns a list regardless of the sample size). Commented May 17, 2017 at 20:59
  • Show the intended output. Commented May 17, 2017 at 21:15
  • What exactly do you want to output here? Choose a random element from a list and return it in a list of size 1? Commented May 17, 2017 at 21:28

2 Answers 2

2

You can use :

from random import choice
foo = [choice(a_list)]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Now that I see it the syntax looks obvious.
0

I'm also starting my journey with Python 3.x and here is my quick code which I just made (you can use it for reference if you get stuck or something):

from random import choice as chc

def hangman(diff):
    #Fill in the words for every difficulty type:
    easy = ['this', 'that']
    medium = ['bicycle', 'bathroom']
    hard = ['superlongword', 'othersuperlongw']
    if diff == 'easy':
        word = chc(easy)
    elif diff == 'medium':
        word = chc(medium)
    elif diff == 'hard':
        word = chc(hard)
    else:
        raise ValueError('Bad difficulty choosen. Terminating.')
    shadow = ['_' for item in word] #list which is showing the progress
    shad_str = ''
    for item in shadow:
        shad_str += item
    print('Choosen word is {} characters long and looks like this:\n{}'.format(len(shad_str), shad_str))
    while '_' in shadow:
        letter = input('Type in next char: ')
        if len(letter) == 1:    #anti-cheat - always have to give 1 char
            if letter in word:
                for i in range(len(word)):   #makes it work correctly even when the letter shows up more than one time
                    if(letter == word[i]):
                        shadow[i] = letter
                temp = ''
                for item in shadow:
                    temp += item
                print(temp)
            else:
                print("This letter isn't in the choosen word! Try again.")
        else:
                print('No cheating! Only one character allowed!')
    else:
        shad_str = ''
        for item in shadow:
            shad_str += item
        print('The game has ended. The word was "{}".'.format(shad_str))

I'm sure the whole thing with the checking can be done in a function (that's why this is a case just for 'easy' mode) so you can call the 'play' function 3 times depending on which difficulty mode you choose.
EDIT: That was not needed, I just noticed you can just decide of the diff with 3 ifs. Now the game works every time if user picks right difficulty.

1 Comment

thanks for this. You're trick to iterate over the word has been helpful while I'm working on my version today.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.