コインの確率
Pythonを使ってコインを100回投げて、表か裏が10回連続で出る確率を求めています。
アプローチとして
①
1,乱数で1か2を出す
2,文字列としてtotalについかする。
3、それをWhileで100回繰り返す。
→100桁の1か2で構成された文字列を得ることができる。
②
1、if in でその文字列の中に1か2が10回連続している箇所があるか調べる。
2、あればcount_timesに1を加える。なければ何もしない
①、②を”任意の数”繰り返す
最後にcount_timesの値を”任意の数”で割る。
値を出す
というアプローチで解こうとしています。
import random
count = 0
total = ""
number = "1111111111"
number_two = "2222222222"
count_times = 0
count_what = 0
result = 0
while count_what < 100:
while count < 100: coin = random.randint(1,2) if coin == 1: total += ("1") count += 1 elif coin == 2: total += ("2") count += 1 else: count = 0 count_what += 1 if total in number or total in number_two: count_times += 1 total = "" else: total = "" result == count_times // 100 print(result)
しかし値が出ません。
ご回答よろしくおねがいします。
回答2件
あなたの回答
tips
プレビュー