-1

The list "inside" is supposed to output a series of "True" and "False" But it outputs "None" instead for all 10 values

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])

Please help!

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 1
    Does this answer your question? [Why is this printing 'None' in the output?](https://stackoverflow.com/questions/28812851/why-is-this-printing-none-in-the-output) – khelwood Jun 11 '20 at 10:44

1 Answers1

0

You have to use return instead of print inside of a function. This should work for you:

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])