首页 > 解决方案 > python随机选择没有重复

问题描述

大家好,我想避免在我的聊天机器人和聊天机器人中出现重复的问题,只需询问列表中的三个问题,然后返回感谢您的帮助

@app.route("/get")
def get_bot_response():
    symptom = request.args.get('msg')
    questions = ['What is altin cuzdan?', 'how we can sell the gold?', 'how we can buy the gold?','who are you?', 'how we can change our password?']
    #time.sleep(5)
    #sampling = random.choice(questions, )
    questions2 = questions[:]
    questions2.remove(quesuions)
    return random.choice(questions2)

标签: pythonpython-3.xloops

解决方案


以这种方式用“提问”列表作为论据吗?正如@usr2564301 所评论的那样,无论哪种方式,您都必须存储在功能之外提出的问题。

@app.route("/get")
def get_bot_response(norepeat):
    symptom = request.args.get('msg')
    questions = ['What is altin cuzdan?', 'how we can sell the gold?', 'how we can buy the gold?','who are you?', 'how we can change our password?']
    #time.sleep(5)
    #sampling = random.choice(questions, )
    questions2 = questions[:]
    questions2.remove(questions)
    while True:
        return_question = random.choice(questions2)
        if return_question in norepeat:
            pass
        else:
            break
    return random.choice(questions2)



norepeat = []
while True:
    question = get_bot_response(norepeat)
    norepeat.append(question)

推荐阅读