Python问题。输入答案并接收在答复列表中随机给出的答复 [英] Python question. Input an answer and recieve a response that is randomly given on a list of responses

查看:163
本文介绍了Python问题。输入答案并接收在答复列表中随机给出的答复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

顺便说一句,使用python 3,但没有标签。我想要完成的是当用户输入一个字母并且如果字母正确时会有一个自动响应,它是正确的,但每次正确时它都不是相同的响应。我希望它放置一个响应列表并使用random.choice随机选择每个正确输入显示的响应。



我的代码到目前为止:

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?

推荐答案

使用 9.6 。 random - 生成伪随机数 - Python 3.6.1文档 [ ^ ]生成随机数的函数。然后,您可以根据数字值选择答案。
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.


这篇关于Python问题。输入答案并接收在答复列表中随机给出的答复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆