Reputation: 53
I'm doing a homework in Anaconda: My question is if can I reassign an integer, for example:
def vol(rad):
for num in range(rad):
num = 1 #I don't know if I'm doing fine assigning the number one or
#should i do it other way.
pi = 3.1416
num += num ** 3 #trying to elevate the num integer to the 3rd potent
rad = 3/4 * (num * pi)
return rad
Once I run it, is just using the number one and I need to know the way to reassign it to use other values once running the function.
Hope you can understand my point and help me.
Thanks in advance.
The code is to calculate the volume of a sphere by the way.
Upvotes: 0
Views: 85
Reputation: 20490
If you see your for loop, you have for num in range(rad):
, which means that num
variable will take values from 0
to rad-1
, but if you do num=1
you will reassign that variable.
In addition, you are also reassigning the variable rad
which you are using in your for loop range in rad = 3/4 * (num * pi)
Also num += num **3
means you are doing num = num + num**3
, whereas I assume you should be doing num = num ** 3
If I understand correctly, and the commentors also pointed out, you need to calculate volume of the sphere with radius rad
, you don't need a for loop for this, you can simply do
def vol(rad):
pi = 3.1416
#Take cube of radius
rad = rad ** 3 # trying to elevate the num integer to the 3rd potent
#Calculate volume
volume = (4 / 3) * (rad * pi)
return volume
Now the output will be
print(vol(4))
#268.0832
print(vol(8))
#2144.6656
As an extra tidbit, we already have a way to get value of pi using math.pi
In [18]: import math
In [19]: print(math.pi)
3.141592653589793
Upvotes: 3