Click here to Skip to main content
15,902,933 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Btw im using python 3 but there is no tag for that. What i want to accomplish is when a user input a letter and if the letter is correct there will be an automatic response that it was correct however it is not the same response every time it is correct. I want it to put a list of responses and use random.choice to randomly pick the response to be shown per correct input.

My code so far:
import random

alphabeth = 'abcdefghijklmnopqrstuvwxyz'
rand = ''
blank = []
rand_list = []
guessed_list = []

def prepWord():
    global rand, guessed_list, blank, rand_list
    words = ['note', 'pencil', 'paper','foo']
    rand = random.choice(words)
    guessed_list = []
    blank = ['_']*len(rand)
    rand_list = []
    for letter in rand:
        rand_list.append(letter)
    startPlay()

def startPlay():
    gameQ = input('Play Hangman? y or n --> ')
    if gameQ == 'y':
        print('Guess the letters:')
        print(blank)
        checkAnswer()
    elif gameQ == 'n':
        print('goodbye')

def playAgain():
    again = input('Would you like to play again? y or n --> ')
    if again == 'y':
        prepWord()
    else:
        Print ('Thanks for playing!')

def checkAnswer():
    x = True
    while x:
        answer = input('').lower()
        if answer not in guessed_list:
            guessed_list.append(answer)
            if len(answer)>1:
                print('One letter at a time.')
            elif answer not in alphabeth:
                print('Invalid character, please try again.')
            else:
                if answer in rand:
                    print ("The letter {} is in the word. Good guess!".format(answer))
                    indices = [ndex for ndex, letter in enumerate(rand_list) if letter == answer]
                    for ndex in indices:
                        blank[ndex] = answer
                    print (blank)
                else:
                    print ("I'm sorry the letter {} is not in the word. Please try again.".format(answer))
        else:
            print('Letter {} already used. Try another.'.format(answer))
        if '_' not in blank:
            print('You win!')
            final_word = ''
            for letter in blank:
                final_word += letter
            print(final_word)
            print('')
            x = False
            playAgain()

prepWord()


What I have tried:

I made a list of responses and placed a random function for the list
reaction=['good job','lucky guess!',you\'re on a roll]
react=random.choice(reaction)


I tried placing it after the
rand_list.append(letter)
if the correct letters has been guessed but somehow it only gives one single response because it is connected to the whole word it will just select another response for another set of word. What i wanted is every input of letter on a specific word once its correct it will give a respone that is not the same as before. Can any one help me?
Posted
Updated 28-May-17 22:51pm

1 solution

Use the 9.6. random — Generate pseudo-random numbers — Python 3.6.1 documentation[^] function to generate random numbers. you can then select your answer based on the number value.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900