我的代码有问题。它输出"None"



列表"inside"应该输出一系列"True"和"False",但它为所有10个值输出"None">

import random 
import math
random.seed(1)
def rand(): 
number = random.uniform(-1,1)
return number
print(rand())
def distance(x, y):
for a,b in x,y:
ans = math.sqrt((x[0] - y[0])**2 + (x[1] - y[1])**2)
return ans
print(distance((0, 0), (1, 1)))
def in_circle(x, origin=(0,0)):
print(distance(x, origin) <1)
print(in_circle((1,1)))  # this is supposed to print only "false" but it prints "False" and "None"
R = 10
inside = [in_circle((rand(), rand())) for i in range(R)]
print(inside[:3])

请帮忙!

您必须在函数中使用 return 而不是打印。 这应该适合您:

import random 
import math
random.seed(1)
def rand(): 
number = random.uniform(-1,1)
return number
print(rand())
def distance(x, y):
for a,b in x,y:
ans = math.sqrt((x[0] - y[0])**2 + (x[1] - y[1])**2)
return ans
print(distance((0, 0), (1, 1)))
def in_circle(x, origin=(0,0)):
return(distance(x, origin) <1)
print(in_circle((1,1)))  # this is supposed to print only "false" but it prints "False" and "None"
R = 10
inside = [in_circle((rand(), rand())) for i in range(R)]
print(inside[:3])

最新更新