Robert Hubbard
Robert Hubbard

Reputation: 66

How to randomize and turn a string into a list in python

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:

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.

Upvotes: 1

Views: 156

Answers (2)

Pedro Lobito
Pedro Lobito

Reputation: 98861

You can use :

from random import choice
foo = [choice(a_list)]

Upvotes: 2

Sqoshu
Sqoshu

Reputation: 1004

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.

Upvotes: 0

Related Questions