Last active
December 23, 2015 00:49
-
-
Save Lysander/6556576 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from random import randint | |
from collections import Counter | |
COIN = { | |
1: "Kopf", | |
2: "Zahl" | |
} | |
DICE6 = {key: str(key) for key in range(1, 7)} | |
EXPERIMENTS = { | |
1: { | |
"item": COIN, | |
"templates": [ | |
"Die Münze wird {} mal geworfen", | |
"Anzahl {}: {} relative Häufigkeit: {}" | |
] | |
}, | |
2: { | |
"item": DICE6, | |
"templates": [ | |
"Der Würfel wird {} mal geworfen", | |
"{} gewürfelt: {} relative Häufigkeit: {}" | |
] | |
} | |
} | |
def generate_distribution(item, count): | |
return Counter( | |
randint(min(item.keys()), max(item.keys())) for _ in range(count) | |
) | |
def calculate_rates(item, distribution, count): | |
return {item[key]: (value, value / count * 100) for (key, value) in | |
distribution.items()} | |
def print_rates(rates, template): | |
for key in sorted(rates.keys()): | |
value, rate = rates[key] | |
print(template.format(key, value, round(rate, 2))) | |
def print_overview(count, template): | |
print(template.format(count)) | |
def run_experiment(item, templates, count): | |
rates = calculate_rates(item, generate_distribution(item, count), count) | |
print_overview(count, templates[0]) | |
print_rates(rates, templates[1]) | |
def ask_for_count(): | |
while True: | |
try: | |
return abs(int(input("Wie oft möchtest Du werfen? "))) | |
except ValueError: | |
print("Bitte nur ganze Zahlen eingeben!") | |
def ask_for_experiment(): | |
while True: | |
try: | |
return int(input(""" | |
Bitte wählen Sie | |
[1] für Münze werfen oder | |
[2] für würfeln: | |
""" | |
)) | |
except ValueError: | |
print("Bitte nur [1] oder [2] wählen!") | |
def main(): | |
choice = ask_for_experiment() | |
experiment = EXPERIMENTS[choice] | |
count = ask_for_count() | |
print() | |
run_experiment(experiment["item"], experiment["templates"], count) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A solution for the problem in: http://www.python-forum.de/viewtopic.php?f=1&t=32298