Added exercise 8

gol
Daniel Knüttel 2018-10-31 14:08:44 +01:00
parent 559bd5dc6f
commit 311f029372
1 changed files with 38 additions and 0 deletions

38
ex_08.py 100644
View File

@ -0,0 +1,38 @@
#!/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))