[PYTHON] A program that answers a few questions and predicts the next answer

I created a program that asks the user three questions and then predicts the answer that the user will answer.

Source

quiz.py


# -*- coding: utf-8 -*-
import random
questions = ["I like cats better than dogs",
        "Science",
        "Man",
        "I like rice better than bread",
        ]
ourAnswers = [[0,1,1,1],
             [1,1,1,0],
             [0,0,1,0],
             [0,1,0,1],
             [1,0,0,1],
             ]


def quiz():
    u"""I have a few questions. Then anticipate the answer you will answer.
    """
    target = random.randint(0,len(questions)-1)
    yourAnswer = len(questions)*[0]
    rates = len(questions)*[0]

    for i, question in enumerate(questions):
        if i == target:
            continue
        yourAnswer[i] = int(raw_input(question+"Is it? 1.Yes,0.No:"))

    for i in xrange(len(questions)):
        i_is1count = 0
        target_is1count = 0
        for ourAns in ourAnswers:
            if ourAns[i] == yourAnswer[i] or i == target:
                i_is1count += 1
                if ourAns[target] == 1:
                    target_is1count += 1
        if i_is1count == 0:
            rates[i] = 0.5
        else:
            rates[i] = float(target_is1count) / i_is1count
    rate = sum(rates)/len(rates)
    if rate >= 0.5:
        yourAnswer[target] = int(raw_input("★ You{0}is not it?(probability{1:.2f}%) 1.Yes,0.No:".format(questions[target],rate*100)))
    else:
        yourAnswer[target] = 1 - int(raw_input("★ You{0}Isn't it?(probability{1:.2f}%) 1.Yes,0.No:".format(questions[target],100-rate*100)))
    ourAnswers.append(yourAnswer[:])


if __name__ == "__main__":
    while(True):
        quiz()

Execution result

Do you like cats better than dogs? 1.Yes,0.No:1
Are you a science student? 1.Yes,0.No:1
Do you like rice better than bread? 1.Yes,0.No:1
★ Are you a man?(Probability 52.50%) 1.Yes,0.No:

Oh, oh ...

Please rewrite the questions (question list) and our Answers (list of answer list) at the beginning and play. I'm getting smarter little by little. The judgment criteria are like this.

Probability that question X is YES = Percentage of answer X that was YES when answer Y was [YES / NO]

We do not weight which question is the one that approaches the core.

Recommended Posts

A program that answers a few questions and predicts the next answer
A program that just presses and releases the Esc key
Try to write a program that abuses the program and sends 100 emails
[Python] A program that rounds the score
A program that searches for the same image
A shell program that displays the Fibonacci sequence
[Python] A program that calculates the number of updates of the highest and lowest records
[Python] A program that counts the number of valleys
A program that asks for a few kilograms to reach BMI and standard weight [Python]
A note that runs an external program in Python and parses the resulting line
[Python] A program that compares the positions of kangaroos.
[Python] A program to find the number of apples and oranges that can be harvested
A program that removes specific characters from the entered text
[Python] A program that finds the most common bird types
[Golang] A program that determines the turn with random numbers
A program that receives the servo command of the radio control, interrupts the Raspberry Pi and logs it
Understand the probabilities and statistics that can be used for progress management with a python program
I made a program that solves the spot the difference in seconds
[Ev3dev] Create a program that captures the LCD (screen) using python
I made a program that automatically calculates the zodiac with tkinter
[Python] A program that rotates the contents of the list to the left
The story of writing a program
[Python] A program that calculates the number of chocolate segments that meet the conditions
A program that will slowly recover the economy from any news headline
A python program that resizes a video and turns it into an image