r/pythonhelp • u/[deleted] • May 04 '21
HOMEWORK Help needed with functions and the random module
import random
import math
def calculate_number(n):
outputs=[]
for j in range(2):
x = random.random()
y = random.random()
print("(",x,",", y,")")
inputs=[x,y]
for i in inputs:
math.sqrt(x**2+y**2)
output=calculate_number(inputs)
return outputHiya, I'm trying to make a function that generates two random numbers between 0 and 1, and then finds their distance from the origin point (after they are plotted)
I assume there are multiple issues with this code, but the one stopping me at the moment is it generates a ton of random numbers, when I only need two.
Any ideas? Cheers
2
Upvotes
1
u/MrPhungx May 04 '21
The problem is that you are calling the function calculate_number inside itself:
output=calculate_number(inputs)This creates an infinite loop
1
u/robohobo- May 04 '21
So you are using recursion where you call a method from inside itself, the error an infinite loop as there is no way for it to stop.
You are also not assigning the value of
math.sqrt(x**2+y**2)to anything.