“三门问题”的代码解法
下面是代码↓ import random,time change_result = [] not_change_result = [] for i in range(1000): a = [[1,0,0],[0,1,0],[0,0,1]]#门的所有可能 b = random.choice(a)#门的列表 c = [1,2,3]#门的编号 d = random.choice(c)#选择的门 print('选择了' + str(d) + '号门') c.remove(d) e = [] for j in c: if b[j-1] == 0: e.append(j) w = random.choice(e) c.remove(w) f = c[0] print(str(w) + '号门是错误的,要换一个门吗') g = random.choice([0,1])#换不换 if g == 0: print('不换') if b[d-1] == 1: print('你选对了') else: print('你选错了') not_change_result.append(b[d-1]) elif g == 1: print('换') if b[f-1] == 1: print('你选对了') else: print('你选错了') change_result.append(b[f-1]) time.sleep(0.2) n = 0 m = 0 for p in change_result: if p == 1: n += 100/len(change_result) for k in not_change_result: if k == 1: m += 100/len(not_change_result) print('选择换的正确率为'+str(n)) print('选择不换的正确率为'+str(m)) 最后算出来n为67%左右,m为32.8%~33%之间 拍屏↓