programming

Xavier Ho contact at xavierho.com
Mon Sep 20 03:13:33 EDT 2010


Thanks Jordan. My reply will be in Blue. I apologise if this ever gets hard
to read, but it's beginning to.

On 20 September 2010 16:51, Jordan Blanton <jeblanton at crimson.ua.edu> wrote:

> "And so he started on a module that estimated the length of a sine wave.
> Here is what he wrote: "
> ok. what is a module?


 A Python module is a .py file. Nothing more.

>  Create a *project0* directory off of your *cs150* directory and move into
> *project0*.
>
> Name your main python file *stopping.py*. Provide one more file named *
> support.py* Dr. Lusth's code should go into the *support.py* file. The
> main function in *stopping.py* should call a function that prompts the
> user of your program for the following information, in the order given.
>
>    - amplitude of the sine wave (in feet)
>    - period of the sine wave (in feet)
>    - distance to the stop sign (in feet)
>    - distance needed to stop without using hand brakes (in feet)
>
> Ok. is there a difference between my main file and the "one more file"?


 Yeah, they are two different files.


> Do I create these by going into the project0 directory and typing "touch
> support.py" and "touch stopping.py"?


Well, I generally just open up a text editor and save it as (for example)
support.py. If you're on Linux, gedit will do the trick. They're just plain
text files.


> What does is mean when is says that the main function in stopping.py should
> *call* a function that prompts the user of your program......?
>

It means the main function in stopping.py should ask for user input in the
terminal. In other words, your program need to be able to read from console
input (stdin in C, or use the input() function in Python) and save them for
calculation.


> "Your main function should call at least one of the functions in the
> support file. You will need to import these functions into your main program
> by placing this line at the top of *stopping.py*.
> Also, your *stopping.py* file should have two function definitions in it,
> the definition of *main* and the definition of a function that obtains
> information from the user. "
> ok. how do i import functions into my main program? what exactly is my main
> program? stopping.py? and how do i define my main program?
>

Ironically, Python doesn't have a "main" function. But it's a generally
accepted practice that you would define something like this:

def main():
    # Do something here

if __name__ == '__main__':
    main()

The first bit is the main function definition, in which you should program
what it is supposed to do. The second bit tells Python to run main() when
the module is run directly (like calling "python stopping.py" without the
quotes to run your program in the terminal.)


>
> Here is part of what is given in the directions: These are codes
> provided...
>
> import math
> #
> # A module for approximating the length of the path traced out
>
> # by a sine wave.
> #
> # written by John C. Lusth
> #
>
> #
> # compute the length of the wavy line traced out by a sine wave
>
> # via approximation. The value of 'step' controls the accuracy of
> # the approximation
>
>
> #
>
> def sineLength(amplitude,period,xDistance,step):
>     x = 0.0
>     total = 0.0
>     lastx = 0
>     lasty = 0
>
>     while x <= xDistance + (step / 2):
>         y = amplitude * math.sin(2 * math.pi / period * x)
>
>
>         d = distance(lastx,lasty,x,y)
>         #print("    distance from (",lastx,",",lasty,") to (",x,",",y,") is",d)
>
>         total = total + d
>         lastx = x
>
>
>         lasty = y
>         x = x + step
>     return total
>
> #
> # compute the distance between point (a,b) and point (b,c)
> #
>
> def distance(a,b,c,d):
>
>     return
>
> What do all the #'s mean?
>
> You really should start by doing some Python tutorials. How to Think Like
a Computer Scientist<http://www.openbookproject.net/thinkcs/python/english2e/>is
a good one. I'm not sure if you're prepared enough to do this project
on
your own.

In short, # is a commet in the file. Same as // in C or Java, if you have
done them before. They're ignored by the compiler, so you can put notes in
there.

> Why are all the values set at 0?
>
>  Because that's how you initialise (and declare) variables in Python. This
way, you can use them later. The initial value in your case is not very
relevant, except for the total.

> What should follow "return" at the very end?
>
>  The function calculates the distance between Point (a, b) and Point (c,
d). I think that's a typo in the comment for that function. Besides, it
should have been a proper doc comment.
Tell your instructor to code like Python, not like C.

You'll want something like:

def distance(a, b, c, d):
    '''Returns the distance between point(a, b) and point(c, d).'''
    # Use Pythagorean
Theorem<http://en.wikipedia.org/wiki/Pythagorean_theorem>here
    # return the distance you calculated  '

Note how I commented the function. This is the standard Python convention,
and you can later on use help(distance) to extract that documentation if
needed.


Seriously, though, do yourself a favour and do some Python tutorial first.
You seem pretty new to Python.

Cheers,
Xav
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100920/becd993d/attachment-0001.html>


More information about the Python-list mailing list