scientific-programming-exer.../ex_08.py
2018-11-07 15:19:36 +01:00

39 lines
780 B
Python

#!/usr/bin/python3
from itertools import count
import random
from util.io import readvalue
def peter_coin_series(randf = random.randint):
result = 0
for k in count(1):
if(randf(0, 1) < 0.5):
return (result, k)
result = 2**k
if( __name__ == "__main__"):
def positive_int(s):
i = int(s)
if(i <= 0):
raise ValueError("{} is negative".format(i))
return i
print("I am going to toss a coin many times.\n"
"You are going to win 2**k if no head appears afer k throws.\n")
bet = readvalue("How many ducats do you want to bet? ", positive_int)
result, throws = peter_coin_series()
win = bet - result
if(win < 0):
print("Bad, A head came out on throw #{}. You lost {} ducats.".format(throws, -win))
else:
print("You won {} ducats.".format(win))