LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   python newbie (https://www.linuxquestions.org/questions/programming-9/python-newbie-225124/)

draetsch 09-01-2004 01:38 AM

python newbie
 
Hi,
I wanted to try some things with python and installed them on my system.
The tutorial went well, but as I wanted to do some math things I get failures.

For example I want to use pi (dump example, but anyway):



Code:

import math
2 * pi

I get the following failure message:

Code:

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in -toplevel-
    2 * pi
NameError: name 'pi' is not defined

Can someone enlighten me with the basic use of modules? :confused:

Proud 09-01-2004 03:55 AM

Check the constant's name is correct. Usually it'd be something like math.PI or whatever.

draetsch 09-01-2004 04:28 AM

yeah,
math.pi did it. Thanks alot:)

Another problem with the same background. When I want to use a function of math i.e. acos() and I use it the old way:

Code:

acos(45)
I get the old failuremessage, ok

but when I try:

Code:

math.acos(45)
I get


Code:

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in -toplevel-
    math.acos(45)
ValueError: math domain error

What did I do wrong?

Proud 09-01-2004 04:31 AM

What's your complete code? Eg.
Code:

import math
result = math.acos(45)
print result


gizmo_thunder 09-01-2004 05:08 AM

acos == arccos ( i.e. cos inverse )
since cos doesnt' take any values above 1 and below -1 its
giving that error.. may be you want to use cos function
to find out the cos value for a given angle.
and the angle has to be in radians most probably.
hope this helps

draetsch 09-01-2004 05:10 AM

I have no code yet, I am only learning at the moment but your code is what I had in mind. But I get a failure:

Code:

>>> import math
>>> result = math.acos(45)

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in -toplevel-
    result = math.acos(45)
ValueError: math domain error
>>> print result

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in -toplevel-
    print result
NameError: name 'result' is not defined


draetsch 09-01-2004 05:16 AM

Oh somebody was faster than I,
yeah, okay I was mathematically wrong.
math.acos(0.5) i.e. works.

Thanks to you all. You helped me very much to get a foot in python.

onnyloh 09-01-2004 09:41 AM

actually u can check a module member by using python prompt.

step is as follow
python #open a prompt
import math # import math module
dir(math) # list math member


All times are GMT -5. The time now is 05:16 PM.