[Tutor] Python Programming Help

Alan Gauld alan.gauld at btinternet.com
Wed Sep 11 11:52:52 CEST 2013


On 11/09/13 02:01, Katie wrote:
> I am a beginner in computer programming.

Hi welcome to the tutor list.

> I am currently trying to write a program in Python version 2.7.5 *that
> uses the math module to compute sinh(x) = 1/2(e^x - e^(-x)) in three
> ways for a given value of x:*
> *1a) by using the sinh function*
> *1b) by using the exp function*
> *1c) by using the value of e and the exponentiation operator

> So, I know that I have to create a NotePad file so that I can import
> that into my command prompt.

You don;t need to import it into the command prompt although
you may for that for testing purposes. But you can run Python
programs directly by double clicking them in your file explorer.

It will help if you tell us which version of Python you are using and 
which Operating System you use. (I'll assume Windows in this post
since you mention Notepad)

Finally, Notepad is fine for short text notes but it's not good for 
programming. You should get a proper programer's editor. IDLE comes
with Python in most cases and if you are using Windows and download
the ActiveState version of Python you will also get Pythonwin.
Either of those will be better than Notepad for writing code.

> In my NotePad file, I have the following...I'm not sure if I am even
> going about doing this problem correctly...
>
> def sinh(x):
>      return (1/2)*(e^x - e^(-x))

The ^ sign does not do what you think in Python. The operator you
need is ** (or use the pow() function).

Also e is not defined directly, it's in the math module so you
need to import math and then reference it as math.e
Alternatively, import e from math:

 >>> from math import e,exp   # import e and exp() from math

And then use them directly:

 >>> import math
 >>> math.exp(3)
20.085536923187668

or

 >>> from math import e,exp
 >>> exp(3)
20.085536923187668

> I am stuck, and don't know where to go from here. I would appreciate
> help please.

You have defined your first version of sinh() you now need to fix
and test it. You can either import it into your python session

 >>> import sinh    # assuming the file is called sinh.py
 >>> sinh.sinh(5)   # remember to include the module name as a prefix

or

You could call the function in your file and run it by clicking in 
explorer. To do that you should add to the end of your file:


print( sinh(2) )   # use any test value you wish...
input('Hit enter to quit')  # pauses output so you can see it


If you are comfortable using the OS command prompt you can also
run it from there with

C:\PATH\TO\YOUR\FILE> python sinh.py

This has the advantage that you will see any error messages too.

Finally, if you use IDLE or Pythonwin you can run the
program from inside those tools using the menus. At this stage
that's probably the best option. You can get a video tutorial
on using IDLE here:

http://www.youtube.com/watch?v=bOvqYw1SZJg


HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list