Help with IMPORTING LIBRARIES into a program

106 visninger
Gå til det første ulæste opslag

murdock...@gmail.com

ulæst,
21. aug. 2016, 22.12.4521.08.2016
til PyScripter
GE all,
New to Python and Pyscripter but not new to programming.  I am using W7 64bit and with (I think) a good working copy of both Python and Pyscripter.  I migrated from Visual C which I could never master for some reason and in fact it was more complicated than I needed. 

At the moment I have loaded a very simple program as seen below:


def main():

    import math

angle = 90
x = cos(angle)
print (x)

if __name__ == '__main__':
    main()

my problem with this short program is that I get an error message that says:

Message    File Name    Line    Position   
Traceback               
    <module>    E:\velocity project\module1.py    17       
NameError: name 'cos' is not defined        

this is annoying the living HE!! out of me because I cant find any documentation that tells me why this is happening nor how to prevent it.
In fact this was one of the reasons I left Visual C..... Because their error messages were cryptic and/or nonexistent... meaning   when you tried
to trace the error code you got it was not listed anywhere.  This is one of the most confusing problems for a programmer that is new to a language.

GUYS PLEASE ... make a composite list of ALL of the error codes and add potential troubleshooting info.

Another thing I found is that when you have a function for example MATH  please tell what library module contains this function so a new programmer can go explore and learn.  In fact the only vendor that ever did this correctly is Borland in Borland C and Borland C++ (in my opinion)      

All help is appreciated... via Email or on the list is OK

M

BBands

ulæst,
22. aug. 2016, 12.38.2422.08.2016
til PyScripter Group
This is not a PyScripter issue. In order to code in python you'll need to grok namespaces...

import math
print(math.cos(1))

or

from math import cos
print(cos(1))

Rather than ranting here, I suggest that you start with the Python tutorial.


    John

Andy Milne

ulæst,
22. aug. 2016, 21.46.0722.08.2016
til pyscr...@googlegroups.com
Well, there's those issues. But,
​ ​
learning how important whitespace is to the python interpreter might be a
​n even​
higher priority.

I think that the tab/spaces before the import statement is scoping the
​import of ​
math to the "main" function.

That import statement is actually the only
​body of code ​
code in the main function. The call to cos() is in the global scope because of the whitespace rules
​ - and as such is the 1st line executed in that program you listed.​

Try something like this:

​import​ math

def main():
    angle = 90
    x = math.cos(angle)
    print(x)

if __name__=='__main__':
    main()

Good Luck!

--
You received this message because you are subscribed to the Google Groups "PyScripter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyscripter+unsubscribe@googlegroups.com.
To post to this group, send email to pyscr...@googlegroups.com.
Visit this group at https://groups.google.com/group/pyscripter.
For more options, visit https://groups.google.com/d/optout.

murdock...@gmail.com

ulæst,
22. aug. 2016, 23.05.0722.08.2016
til PyScripter
John, Andy,
First let me thank you both for the response. John you have never seen me rant and  before I ever posted here I tried all of those suggestions and yes I read the manual before that too.  Like I said I am not new to programming as I am a retired electrical engineer.. or to be more precise a computer engineer as they call them these days.

Andy please forgive me but I a a bit slow where white space is concerned and I will reread that section of the book again you can be sure.

I will retry all of the suggestions you guys have put forth and then if they still dont work I will reinstall the entire python/pyscripter setup..

thanks again
M
Svar alle
Svar til forfatter
Videresend
0 nye opslag