This is my code...it errors out on line 5 and I don't know why. Thanks for the help in advance.
The error is invalid syntax.
from math import pi
radius = int(raw_input(("What radius would you like?"))
def area_of_circle(radius):
return (radius ** 2) * math.pi
area_of_circle(radius)
Instead of
from math import pi
just use
import math
from math import pi loads the math module, but instead of assigning the module to the name math, it looks up pi in the module and assigns that to the name pi. This can be convenient, but it can also cause horrible initialization order issues.
I believe you're missing a closing parenthesis on line 2. But as Marius mentioned, you don't actually need those parentheses.
from math import pi
radius = int(raw_input("What radius would you like?")) # Parenthesis removed
def area_of_circle(radius):
return (radius ** 2) * math.pi
area_of_circle(radius)
On top of that, you should also look at the suggestion by #user2357112 and use import math and math.pi. The updated code will look like:
import math # Not import pi
radius = int(raw_input("What radius would you like?")) # Parenthesis removed
def area_of_circle(radius):
return (radius ** 2) * math.pi
# Printing here for feedback
print area_of_circle(radius)
it works fine
import math
radius = int(raw_input(("What radius would you like?")))
def area_of_circle(radius):
return (radius ** 2) * math.pi
print area_of_circle(radius)
you were missing ")" close parenthesis and the imported library should be math not pi.
import math
#parameter
radius = int(input("Enter the radius: "))
def AreaOfCircle(radius):
return (radius**2)*math.pi
print(AreaOfCirle(radius))
Related
Why I have tried:
def sphere_area(radius: float):
area = float(4 * math.pi * radius * radius)
return area
def sphere_volume(radius: float):
volume = float((4 / 3) * math.pi * math.pow(radius, 3))
return volume
Expected output:
surface_area: #float number
volume: #float
If you want to output the area and volume of a sphere with a given radius, you need to call the functions you have defined.
For example, if you wanted the volume and surface area of a sphere with radius 2:
print(sphere_area(2))
print(sphere_volume(2))
We call the function name, put pass in our radius value, and print it out since we will only be returned a float value.
If you also wish, you can store the function values inside of two other variables:
sp_area = sphere_area(2)
sp_volume = sphere_area(2)
Remember to use import math at the top of your code so your code works when you use math functions.
I hope this helped! Let me know if you need any further clarifications or details :)
When trying to evaluate a function f(x) with Sympy, the final output appears to keep sin(n * pi) and cos (n * pi) in symbolic form but does not evaluate them as 0 and (-1)^n respectively when n is defined to be positive integers. However, Symbolab appears to be able to do the above behavior.
Is there a way to get Sympy evaluate expression similar to what Symbolab does?
Minimal example to generate the behavior in Sympy
# Load libraries
import sympy as sp
from sympy import*
from IPython.display import display
init_printing()
# To turn off bugging warnings
import warnings
warnings.filterwarnings('ignore')
x,L,pi = symbols('x, L, pi', real = True, positive = true)
n = symbols('n', positive = True, integer=True)
a_n_x = 2/L
a_n_in = x * cos((n*pi*x)/L)
display(a_n_in)
a_n = a_n_x * integrate(a_n_in, (x,0,2))
display(a_n)
a_n = a_n.subs(L,2)
display(a_n)
The problem is on this line:
x,L,pi = symbols('x, L, pi', real = True, positive = true)
This defines pi as a positive real variable, so it is treated just like any other positive real variable would be - in particular, sin(n * pi) and cos(n * pi) cannot be simplified, any more than sin(n * x) or cos(n * x) could be. The fact that you named it pi doesn't matter.
To fix it, use the symbol pi defined in the sympy module itself, which SymPy understands to mean the constant π. This is already imported in the line from sympy import *, so you just need to not replace it with your own variable.
You need to ensure n is an integer, otherwise the identity does not hold:
n = symbols('n', integer=True)
trigsimp( cos(pi * n) ) # Prints (-1)**n
In order to get the desired result, we have to use sympy.cos() and sympy.pi. The updated minimal reproduceable example demonstrates this convention in further details
# Load libraries
import sympy as sp
from sympy import*
from IPython.display import display
init_printing()
# To turn off bugging warnings
import warnings
warnings.filterwarnings('ignore')
x,L = symbols('x, L', real = True, positive = True)
n = symbols('n', positive = True, integer=True)
a_n_x = 2/L
a_n_in = x * sp.cos((n*pi*x)/L)
display(a_n_in)
a_n = a_n_x * integrate(a_n_in, (x,0,2))
display(a_n)
a_n = a_n.subs(L,2)
display(a_n.simplify())
EDIT: 12/4/2021 based on detailed explanation of how pi is defined in SymPy by #kaya3
I am trying to make a basic tool to make my everyday easier, solving some assignments for me. Unfortunately, I can't figure out how to make it calculate in degrees when tangent is being used.
My code:
import math
class Astro():
def start(self):
velocity = input("What is the galaxy's velocity? (m/s) \n")
peculiar = (float(velocity) - 938600) ** 2
mass = (3 * float(peculiar) * (10 ** 11) * 50 * (10 ** 6) * (8 * (180 / math.pi))
* 9.46 * (10 ** 15)) / (2 * 6.67 * (10 ** -11))
print("The galaxy's mass is " + str(mass) + " kg. \n")
if __name__ == '__main__':
sup = Astro()
sup.start()
EDIT: Sorry for the lack of context; this is about calculating the masses of galaxies using 2 functions, the first one, line 7 to get the peculiar velocity, and the second one in lines 8-9 to get the actual mass of the considered galaxy.
SOLVED: math.tan(8 * pi / 180)
Thank you for all your help!
Computers work in radians. Try
answer = tan(angle * pi / 180)
to use your angle in degrees into a trig function. Or try
answer = atan(number) * 180 / pi
to get answer in degrees.
The math package has the functions radians and degrees but under the hood these are just:
def radians(deg):
return deg * pi / 180
def degrees(rad):
return rad * 180 / pi
Here is a wrapper you can use to make degree-using trig functions (just had it lying around somewhere, although I use numpy instead of math)
import math
import itertools
import functools
def _use_deg(f, arc = False):
if not arc:
def df(*args):
args = list(args)
for index, value in enumerate(args):
try:
args[index] = math.radians(value)
except TypeError:
pass
return f(*args)
else:
def df(*args):
return math.degrees(f(*args))
return functools.wraps(f)(df)
sind = _use_deg(math.sin)
cosd = _use_deg(math.cos)
tand = _use_deg(math.tan)
arcsind = _use_deg(math.asin, True)
arccosd = _use_deg(math.acos, True)
arctand = _use_deg(math.atan, True)
arctan2d = _use_deg(math.atan2, True)
You don't want to get in a fight with the math library. Let the math library give you an answer in radians, then multiply it's answer by 180/math.pi to get degrees.
Here is my code thus far,
from math import *
def main():
sides = eval(input("Enter the number of sides:"))
value = 360/(2 * sides)
sinvalue = sin(value)
PI = sinvalue * sides
print("Approximate value of pi =", PI)
However, I am not getting the proper value that is in the sample code.
math.sin expects angles to be specified in radians.
>>> print(math.sin.__doc__)
sin(x)
Return the sine of x (measured in radians).
You are specifying it in degrees. Specify the angle in radians instead:
value = math.pi / sides
Or, if you don't want to use math.pi so explicitly, use the helper function to convert units:
value = math.radians(360/(2*sides))
In R we have a function, distCosine, within a package Geosphere, here
I am current;y looking to find a similar function in Python, but all I can find is SciPy's implementation here. This does not include the radius.
Is there a Python based working of this R function? Else I have found some to work write out one myself
You can see the source code for the R distCosine function... just write one in python:
from math import acos, sin, cos, pi
def distCosine(p1, p2, r=6378137):
p1 = [p * pi / 180 for p in p1]
p2 = [p * pi / 180 for p in p2]
out = acos(sin(p1[1]) * sin(p2[1]) +\
cos(p1[1]) * cos(p2[1]) *\
cos(p1[0] - p2[0])) * r
return out
distCosine([0, 0], [90, 90])
# 10018754.171394622
You might want to add some error checking of sorts, but this works on the example provided by the R package...