Skip to content

Instantly share code, notes, and snippets.

@kurehajime
Created December 8, 2013 06:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurehajime/7854107 to your computer and use it in GitHub Desktop.
Save kurehajime/7854107 to your computer and use it in GitHub Desktop.
いくつかの質問に答えると、次の回答を予想するプログラム ref: http://qiita.com/kurehajime/items/ca0a90ba063a23b714fc
犬より猫が好きですか? 1.はい,0.いいえ :1
理系ですか? 1.はい,0.いいえ :1
パンよりご飯が好きですか? 1.はい,0.いいえ :1
★あなたは男ですね?(確率52.50%) 1.はい,0.いいえ :
# -*- coding: utf-8 -*-
import random
questions = ["犬より猫が好き",
"理系",
"男",
"パンよりご飯が好き",
]
ourAnswers = [[0,1,1,1],
[1,1,1,0],
[0,0,1,0],
[0,1,0,1],
[1,0,0,1],
]
def quiz():
u"""いくつか質問をします。その後、あなたが答えるであろう回答を予想します。
"""
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+"ですか? 1.はい,0.いいえ :"))
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("★あなたは{0}ですね?(確率{1:.2f}%) 1.はい,0.いいえ :".format(questions[target],rate*100)))
else:
yourAnswer[target] = 1 - int(raw_input("★あなたは{0}ではないですね?(確率{1:.2f}%) 1.はい,0.いいえ :".format(questions[target],100-rate*100)))
ourAnswers.append(yourAnswer[:])
if __name__ == "__main__":
while(True):
quiz()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment