Python :: Aufgabe #265
5 Lösungen
Häufigkeit einer 6 beim Würfeln?!
Anfänger - Python
von DragStar
- 06.04.2020 um 08:34 Uhr
Verfassen Sie ein Programm, welches 25.000 mal, jeweils so lange das Würfeln mit einem Würfel simuliert, bis eine 6 erscheint.
Auszugeben ist, wie oft man dabei schlimmstenfalls würfeln musste.
Auszugeben ist, wie oft man dabei schlimmstenfalls würfeln musste.
Lösungen:
import random
würfel = 0
versuche = 0
for i in range(0,25000):
würfel = random.randint(1,6)
versuche = versuche+1
if würfel == 6:
break
print("So viele Versuche hat es gebraucht:",versuche)
import random
z = 0
z1=0
for i in range(25000):
z += 1
zahl = random.randint(1,6)
if zahl == 6 and z > z1:
z1=z
z=0
print(z1)
import random
dice_max = 25000
dice = 0
glob_counter = 0
counter_6 = 0
for i in range(0, dice_max):
random.seed()
dice = random.randint(1, 6)
glob_counter += 1
if dice == 6:
counter_6 += 1
print("Bisher", glob_counter, "Versuche")
input()
print("Die 6 wurde", counter_6, "x gewürfelt von", dice_max, "Versuche")
import random
global_highest = 0
for i in range(25000):
n = 0
while random.randint(1, 6) != 6:
n += 1
if n > global_highest:
global_highest = n
print(global_highest)
import random
_anzahl = input("Bitte Anzahl an Wuerfen eingeben: ")
trys = 0
for i in range(int(_anzahl)):
check = False
while not check:
wuerfel = random.randint(1,6)
trys += 1
if wuerfel == 6:
check = True
print(f"Es wurden {trys} Versuche benoetigt.")