Python question. Please help.

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Python question. Please help.

Post by Good-will on Sun May 28, 2017 3:13 pm
([msg=93732]see Python question. Please help.[/msg])

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:
Code: Select all
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()




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

I tried placing it after the
Code: Select all
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?
Good-will
New User
New User
 
Posts: 2
Joined: Fri May 19, 2017 6:48 pm
Blog: View Blog (0)


Re: Python question. Please help.

Post by AkulaClass on Mon May 29, 2017 2:23 pm
([msg=93735]see Re: Python question. Please help.[/msg])

I am guessing you need to declare the variables it in prepwords and use it later on in checkanswer, heres something I did where I added class instead of either making the react a global variable or declaring inside checkanswer.
https://gist.github.com/rikoudosenin/c04aa58753c832bc73fa268e2253fbe4
AkulaClass
New User
New User
 
Posts: 1
Joined: Mon May 29, 2017 2:19 pm
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests