嗨,伙计们,我是Python的业余爱好者。 下面是我编写的代码。你知道吗
for _ in range(10):
x = random.random()
print("Your x coordinates are",x)
y = random.random()
print("Your y coordinates are",y)
distance = x**2 + y**2
distance2 = math.sqrt(distance)
print("The distance of your pair of points is",distance2)
它打印10个随机生成的坐标,并使用毕达哥拉斯(montecarlo模拟)计算每对坐标的距离。 我现在创建了一个名为:
inside = 0
每当一对坐标的距离在0和1之间时,此计数器需要递增1。如果距离大于1,计数器将不递增。我曾经尝试过使用while循环和if语句,但是我没有掌握其中的诀窍。有人能帮忙吗?你知道吗
谨致问候。你知道吗
您可以使用类似于my answer到your last question的方法:
您只需通过sum()从数据中总结出所有正确(=1)的内容:
输出:
那应该能解决你的问题!你知道吗
编辑:我删除了
0 <=
检查,因为它是一个平方根,它总是大于0我试过你的方法。如果我理解正确,你要计算0和1之间的距离。你知道吗
打印(计数器)
相关问题 更多 >
编程相关推荐