I created a program that asks the user three questions and then predicts the answer that the user will answer.
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()
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