Can´t find the problem with this code, at the end its supossed to print pi but it print numbers close to 2 instead
import random
def kasta_pil():
x = random.random()
y = random.random()
return (x, y)
def inne_i_cirkel(x, y):
if x + y <= 1:
return True
else:
return False
while True:
try:
z = int(raw_input("Hur många pilar vill du kasta: "))
except ValueError:
print "Invalid input"
continue
if z < 1:
print "Måste vara åtminstone 1"
else:
break
pilar_i_cirkel = 0.0
for i in range(z):
x, y = kasta_pil()
if inne_i_cirkel(x, y):
pilar_i_cirkel = pilar_i_cirkel + 1
print "Pi är ungefär: ", (pilar_i_cirkel / z) * 4
Aucun commentaire:
Enregistrer un commentaire