Re: [Tutor] A better way for greatest common divisor

2010-07-31 Thread David Hutto
On Sat, Jul 31, 2010 at 12:43 AM, David Hutto  wrote:
> On Sat, Jul 31, 2010 at 12:37 AM, David Hutto  wrote:
>> On Fri, Jul 30, 2010 at 11:51 PM, Steven D'Aprano  
>> wrote:
>>> On Sat, 31 Jul 2010 01:03:27 pm David Hutto wrote:
>>>
 This fixes the floating point 'bug' when numerator is greater than
 denominator: http://python.pastebin.com/bJ5UzsBE
>>>
>>> I don't mean to be disparaging ... ah hell, who am I fooling? Yes I do.
>>> What is that mess? *wink*
>> It works except under [3], and that's fixable. And even, I know it's a
>> good gradumacated the eighth grade, newbie attempt.*winks* back.
>>>
>>> I can see at least four problems with that:
>>>
>>> 1. You have a function called "gcd" that doesn't calculate the gcd, but
>>> does something else as well. That makes it a misleading name.
>>
>> I still have the habit of wanting to use the functions like I would an
>> instance of the functions.
>>
>>>
>>> 2. The principles of code reuse and encapsulation suggest that each
>>> function should (as much as possible) do one thing, and do it well. You
>>> have a function that tries to do two or three things. You should have a
>>> single function to calculate the gcd, and a second function to use the
>>> gcd for reducing a fraction as needed, and potentially a third function
>>> to report the results to the user.
>>
>> Then maybe I should have done a larger class of functions instead then?
>>
>>
>>>
>>> 3. Your function has a serious bug. To see it, call gcd(5, 5) and see
>>> what it doesn't do.
>>
>> I'll get to it, but it seems like I had that in the original, not the
>> revised, maybe not, but did all other test cases for it, other than
>> that
>>
>>
>>
>>>
>>> 4. Code duplication. Your function repeats fairly major chunks of code.
>>> Copy-and-paste programming is one of the Deadly Sins for programmers.
>>> The way to get rid of that is by encapsulating code in functions (see
>>> point 1 above).
>>
>> I thought about putting certain print statements in a function, as
>> well as seperating the gcd into a from fractions import *, with a
>> different parameter for each if the returning if placed it into the
>> called function, but it seemed a little *overkill*, when it worked
>> well within the single function, with a few parameters placed in
>> through an instance with input.
>>
>>>
>>>
>>>
>>> --
>>> Steven D'Aprano
>>> ___
>>> Tutor maillist  -  tu...@python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>>
>
> But, just to say, it started out as just trying to calculate the GCD
> with your current python skills(not import fractions, and
> print(gcd(3,9))for a practice exercise, and the rest is just the
> input, and stated output.
>

however, had it been a math exercise, I would have sucked in boiling
it down, initially.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] string editing

2010-07-31 Thread ANKUR AGGARWAL
hey although i knw string are mutable but i want to know if there is anyway
to add in the string.
like i m wrking on the tool and want to run the input from the user in the
terminal

like user makes input "this is ankur"
and i want it like "this\ is\ ankur"  ???
is it possible??
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string editing

2010-07-31 Thread Mark Lawrence

On 31/07/2010 08:50, ANKUR AGGARWAL wrote:

hey although i knw string are mutable but i want to know if there is anyway

   ^^^ immutable!

to add in the string.
like i m wrking on the tool and want to run the input from the user in the
terminal

like user makes input "this is ankur"
and i want it like "this\ is\ ankur"  ???
is it possible??




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


>>> ankur = "this is ankur"
>>> ankur.replace(' ', '\ ')
'this\\ is\\ ankur'

HTH.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Simple Python Program

2010-07-31 Thread Evert Rol
> Can anyone tell me how to fix the errors I am getting if possible? I'm quite 
> new and so confused...

You're not telling us what the errors are. If you'd like us help to debug your 
program, best is to provide information about how you run it, and what output 
you're getting. Not just the program (sure, we can all copy-paste the program 
and run it, but we're not all going to do that). Just copy-paste the error 
message along with the rest of the email a next time.
Python is pretty verbose and clear with its error messages; once you learn to 
read the traceback (bottom-up), you can often figure out what's wrong: read the 
exact error message and line number, and try to understand why you get that 
message.

That said, I can see one glaring error when skimming through your code (unless 
I'm horribly misreading things):
 
>  import random
> def main():
> print
> 
> playerOne, playerTwo = inputNames(playerOne, PlayerTwo)
>  
> while endProgram == 'no':

You use endProgram to compare with 'no' here, but
> 
> endProgram == no

you only set the variable here.

This is programming 101 (not even Python specific): declare (& set) your 
variables before you use them (in comparisons, function calls, etc).

Also:
> playerOne = 'NO NAME'
> playerTwo = 'NO NAME'

are set here to 'NO NAME' (some default value), but *before* that, you assign 
real names. Since the default assignment is after the proper assignment, you 
end up with 'NO NAME' for both players (except that your program currently 
crashes at the while statement).

Finally:
> 
> also how do I display the winner under the displayInfo function?
 
If you want to do that, you'll have to feed the displayInfo() function the name 
of the winner as an argument.
What you do below in the second line, is assign the displayInfo function to 
winnerName. Instead, you'll have to call that function, with the winnerName as 
as argument (you're doing this correctly before, so it's somewhat odd you're 
doing it wrong here).

> winnerName = rollDice(p1number, p2number, playerOne, playerTwo, 
> winnerName)
> winnerName = displayInfo

All in all, I can see why you're "so confused". Do you understand what you're 
doing; specifically, do you understand the flow of the program? Or did you just 
copy-paste something and made a few edits, then gave it a try? 
Perhaps you should take a step back, start even a bit smaller (single function 
programs), read a few other tutorials, then come back to this program.
You're most welcome to ask questions here, but looking through your code, 
there's a few generic programming mistakes here that you should learn about 
first.

Good luck,

  Evert


> endProgram = raw_input('Do you want to end program? (Enter yes or 
> no): ')
> 
> def inputNames(playerOne, PlayerTwo):
> 
> p1name = raw_input("Enter your name.")
> p2name = raw_input("Enter your name.")
> return playerOne, playerTwo
> 
> def random(winnerName):
> 
> p1number = random.randint(1, 6)
> p2number = random.randint(1, 6)
> 
> if p1number > p2number:
> playerOne = winnerName
> elif p1number < p2number:
> playerTwo = winnerName
> else p1number == p2number:
> playerOne, playerTwo = winnerName
> 
> def displayInfo():
> 
> #how do I display winner?
> 
> main()
> 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string editing

2010-07-31 Thread Alan Gauld


"ANKUR AGGARWAL"  wrote

hey although i knw string are mutable but i want to know if there is 
anyway

to add in the string.


Although strings are immutable there are numerous ways to create a new
variation on a string. For your purposes the replace() method is 
probably

best but if you do


help(str)


you can read about all the options.

You might also find the Handling Text topic of my tutorial helpful.

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A better way for greatest common divisor

2010-07-31 Thread David Hutto
On Sat, Jul 31, 2010 at 3:39 AM, David Hutto  wrote:
> On Sat, Jul 31, 2010 at 12:43 AM, David Hutto  wrote:
>> On Sat, Jul 31, 2010 at 12:37 AM, David Hutto  wrote:
>>> On Fri, Jul 30, 2010 at 11:51 PM, Steven D'Aprano  
>>> wrote:
 On Sat, 31 Jul 2010 01:03:27 pm David Hutto wrote:

> This fixes the floating point 'bug' when numerator is greater than
> denominator: http://python.pastebin.com/bJ5UzsBE

 I don't mean to be disparaging ... ah hell, who am I fooling? Yes I do.
 What is that mess? *wink*
>>> It works except under [3], and that's fixable. And even, I know it's a
>>> good gradumacated the eighth grade, newbie attempt.*winks* back.

 I can see at least four problems with that:

 1. You have a function called "gcd" that doesn't calculate the gcd, but
 does something else as well. That makes it a misleading name.
>>>
>>> I still have the habit of wanting to use the functions like I would an
>>> instance of the functions.
>>>

 2. The principles of code reuse and encapsulation suggest that each
 function should (as much as possible) do one thing, and do it well. You
 have a function that tries to do two or three things. You should have a
 single function to calculate the gcd, and a second function to use the
 gcd for reducing a fraction as needed, and potentially a third function
 to report the results to the user.
>>>
>>> Then maybe I should have done a larger class of functions instead then?
>>>
>>>

 3. Your function has a serious bug. To see it, call gcd(5, 5) and see
 what it doesn't do.
>>>
>>> I'll get to it, but it seems like I had that in the original, not the
>>> revised, maybe not, but did all other test cases for it, other than
>>> that
>>>
>>>
>>>

 4. Code duplication. Your function repeats fairly major chunks of code.
 Copy-and-paste programming is one of the Deadly Sins for programmers.
 The way to get rid of that is by encapsulating code in functions (see
 point 1 above).
>>>
>>> I thought about putting certain print statements in a function, as
>>> well as seperating the gcd into a from fractions import *, with a
>>> different parameter for each if the returning if placed it into the
>>> called function, but it seemed a little *overkill*, when it worked
>>> well within the single function, with a few parameters placed in
>>> through an instance with input.
>>>



 --
 Steven D'Aprano
 ___
 Tutor maillist  -  tu...@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor

>>>
>>
>> But, just to say, it started out as just trying to calculate the GCD
>> with your current python skills(not import fractions, and
>> print(gcd(3,9))for a practice exercise, and the rest is just the
>> input, and stated output.
>>
>
> however, had it been a math exercise, I would have sucked in boiling
> it down, initially.
>

There is a difference between defining a function as a singular
activity needed to be performed, and a function that serves as a tool,
that asks for input and output, as well as serves several
utilizations, e.g., it not only accepts the numerator/denominator, but
spits out the gcd, the reduced, and the whole numeber reduced if the
numerator is greater. but if they are even, will come with a little if
num == den boolean, when I'm finished .
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem with simple script

2010-07-31 Thread Richard D. Moores
On Thu, Jul 29, 2010 at 12:22, Richard D. Moores  wrote:
> On Wed, Jul 28, 2010 at 08:35, Richard D. Moores  wrote:
>
> Here's my slight revision of Steven's script (see my note, lines 9-14)
> -- revised only because I wanted it to handle the case where the user
> (me) entered the interval bounds in the wrong order, e.g., 10, 5
> instead of 5, 10.  I've also added a way ('r') for the user to choose
> to get another random number in the same interval without having to
> reenter the bounds: . Then I
> needed a way ('c') to choose to change the bounds after 'r' had been
> used. I used a flag (lines 42, 53, 55) to accomplish these additions.
> Was there a better way? Should I have written a couple of extra
> functions instead of using the flag? Are flags unpythonic?

So here I am talking to myself again. :)

Well, whether flags are unpythonic or not, I was finally able to get
rid of them. See 

Dick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A better way for greatest common divisor

2010-07-31 Thread Richard D. Moores
>> On Sat, 31 Jul 2010 01:03:27 pm David Hutto wrote:

> There is a difference between defining a function as a singular
> activity needed to be performed, and a function that serves as a tool,
> that asks for input and output, as well as serves several
> utilizations, e.g., it not only accepts the numerator/denominator, but
> spits out the gcd, the reduced, and the whole numeber reduced if the
> numerator is greater. but if they are even, will come with a little if
> num == den boolean, when I'm finished .

Of course you can do as you wish, but how about breaking up that
function as Steven suggests, and using the script, the module that
contains them as the tool you desired?

Dick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem with subprocess

2010-07-31 Thread Mac Ryan
On Fri, 2010-07-30 at 14:36 +0200, Bala subramanian wrote:
> I have to do a series of job in a remote machine. I put each job in a
> text file called 'job' as and wrote the following code that can read
> each line in the text file and execute the job.

If that works for you, no need to change it, of course. 

Just wondered - however - if you are aware that there is a python
library that just do that (remote machine management). It's called
"fabric":

>From the official docs: "Fabric is a Python library and command-line
tool for streamlining the use of SSH for application deployment or
systems administration tasks.

It provides a basic suite of operations for executing local or remote
shell commands (normally or via sudo) and uploading/downloading files,
as well as auxiliary functionality such as prompting the running user
for input, or aborting execution.

Typical use involves creating a Python module containing one or more
functions, then executing them via the fab command-line tool."

http://docs.fabfile.org/0.9.1/



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python - RPG Combat System

2010-07-31 Thread bob gailer

On 7/30/2010 10:49 PM, Jason MacFiggen wrote:

what can I do instead of writing print so many times?
import random
my_hp = 50
mo_hp = 50
my_dmg = random.randrange(1, 20)
mo_dmg = random.randrange(1, 20)
while True:
if mo_hp < 0:
print "The Lich King has been slain!"
elif my_hp < 0:
print "You have been slain by the Lich King!"
if mo_hp <= 0:
break
elif my_hp <= 0:
break
else:
print "Menu Selections: "
print "1 - Attack"
print "2 - Defend"
print
choice = input ("Enter your selection. ")
choice = float(choice)
print
if choice == 1:
mo_hp = mo_hp - my_dmg
print "The Lich King is at ", mo_hp, "Hit Points"
print "You did ", my_dmg, "damage!"
print
my_hp = my_hp - mo_dmg
print "I was attacked by the lk for ", mo_dmg," damage!"
print "My Hit Points are ", my_hp
print
elif choice == 2:
mo_hp = mo_hp - my_dmg / 2
print "The Lich King is at", mo_hp, "Hit Points"
print "you did ", my_dmg / 2, "damage!"
print
my_hp = my_hp - mo_dmg
print "I was attacked by the lk for ", mo_dmg," damage!"
print "My Hit Points are ", my_hp
print


Most of the statements in each choice block are identical. Factor them 
out, giving:


if choice == 1:
mo_hp = mo_hp - my_dmg
print "you did ", my_dmg /, "damage!"
elif choice == 2:
mo_hp = mo_hp - my_dmg / 2
print "you did ", my_dmg / 2, "damage!"
print "The Lich King is at", mo_hp, "Hit Points"
my_hp = my_hp - mo_dmg
print "You did ", my_dmg, "damage!"
print
print "I was attacked by the lk for ", mo_dmg," damage!"
print "My Hit Points are ", my_hp
print

You could (better) move these statements into a function, passing 1 or 2 
as the divisor for my_dmg and returning the updated values for my_ho and 
my_hp.


def update(factor):
print "The Lich King is at", mo_hp, "Hit Points"
print "you did ", my_dmg / factor, "damage!"
print
print "I was attacked by the lk for ", mo_dmg," damage!"
print "My Hit Points are ", my_hp
print
return mo_hp - my_dmg / factor, my_hp - mo_dmg
...
if choice == 1:
mo_hp, my_hp = update(1)
elif choice == 2:
mo_hp, my_hp = update(2)

--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python - RPG Combat System

2010-07-31 Thread David Hutto
On Sat, Jul 31, 2010 at 1:51 PM, bob gailer  wrote:
> On 7/30/2010 10:49 PM, Jason MacFiggen wrote:
>>
>> what can I do instead of writing print so many times?
>> import random
>>    my_hp = 50
>>    mo_hp = 50
>>    my_dmg = random.randrange(1, 20)
>>    mo_dmg = random.randrange(1, 20)
>>    while True:
>>        if mo_hp < 0:
>>            print "The Lich King has been slain!"
>>        elif my_hp < 0:
>>            print "You have been slain by the Lich King!"
>>        if mo_hp <= 0:
>>            break
>>        elif my_hp <= 0:
>>            break
>>        else:
>>            print "Menu Selections: "
>>            print "1 - Attack"
>>            print "2 - Defend"
>>            print
>>            choice = input ("Enter your selection. ")
>>            choice = float(choice)
>>            print
>>        if choice == 1:
>>            mo_hp = mo_hp - my_dmg
>>            print "The Lich King is at ", mo_hp, "Hit Points"
>>            print "You did ", my_dmg, "damage!"
>>            print
>>            my_hp = my_hp - mo_dmg
>>            print "I was attacked by the lk for ", mo_dmg," damage!"
>>            print "My Hit Points are ", my_hp
>>            print
>>        elif choice == 2:
>>            mo_hp = mo_hp - my_dmg / 2
>>            print "The Lich King is at", mo_hp, "Hit Points"
>>            print "you did ", my_dmg / 2, "damage!"
>>            print
>>            my_hp = my_hp - mo_dmg
>>            print "I was attacked by the lk for ", mo_dmg," damage!"
>>            print "My Hit Points are ", my_hp
>>            print
>
> Most of the statements in each choice block are identical. Factor them out,
> giving:
>
>        if choice == 1:
>            mo_hp = mo_hp - my_dmg
>            print "you did ", my_dmg /, "damage!"
>        elif choice == 2:
>            mo_hp = mo_hp - my_dmg / 2
>            print "you did ", my_dmg / 2, "damage!"
>        print "The Lich King is at", mo_hp, "Hit Points"
>        my_hp = my_hp - mo_dmg
>        print "You did ", my_dmg, "damage!"
>        print
>        print "I was attacked by the lk for ", mo_dmg," damage!"
>        print "My Hit Points are ", my_hp
>        print
>
> You could (better) move these statements into a function, passing 1 or 2 as
> the divisor for my_dmg and returning the updated values for my_ho

You mean my_dmg I think.

 and my_hp.
>
> def update(factor):
>    print "The Lich King is at", mo_hp, "Hit Points"
>    print "you did ", my_dmg / factor, "damage!"
>    print
>    print "I was attacked by the lk for ", mo_dmg," damage!"
>    print "My Hit Points are ", my_hp
>    print
>    return mo_hp - my_dmg / factor, my_hp - mo_dmg
> ...
>        if choice == 1:
>            mo_hp, my_hp = update(1)
>        elif choice == 2:
>            mo_hp, my_hp = update(2)
>
> --
> Bob Gailer
> 919-636-4239
> Chapel Hill NC
>
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Writing scripts and apps for Internet consumption

2010-07-31 Thread Eric Hamiter
Not sure if this is the right place for this, since this is a tutor list,
but I think it is because it involves learning Python and the application of
knowledge.

I've just started learning it as my initial programming language as of two
months ago. I like to think I'm making steady progress, and I now understand
the most rudimentary level of the basics. What I keep reading is how Python
is most powerful on server side applications, in the cloud, so to speak. The
portability of Python is also widely evangelized.

Here's my problem with this so far-- I can write a basic script, have it
take in data, rearrange it, and spit it back out. Following along in a book,
I can write a basic GUI or game. It's all wine and roses on my Windows
laptop, where I have everything configured just right, with all of the
modules in place where they need to be.

Moving this to a server or even another computer so far has been a seemingly
impossible task. There's a lot of documentation for CGI scripting (which is
now frowned upon, with every page recommending looking into wsgi), and there
have been applications devoted to transforming scripts into Windows
executables (py2exe, etc.) but it seems like this is much more confusing
than need be, and I can't get them to work regardless. When I try and google
for solutions, choosing any terms like "web" or "server" point me to massive
framework solutions like Django or Pylons, which seem extraordinarily
complex for what I want.

Specific examples: I have a livewires/pygame GUI game I wrote along with
folowing the book "Python Programming for the Absolute Beginner" and it
works great on my laptop. I tried installing Python/pygame on a work
computer and copying my scripts over, but it endlessly fails with errors so
obtuse I can't troubleshoot. I'm not even sure if I have the correct modules
installed here. Should they be under "Lib" or "libs" or "includes"?  Trying
to use py2exe fails because I can't figure out how to include non-scripts in
the final program, like .pngs or .jpgs. How would I even begin to put this
on a server? I'm clueless.

Another program I am using on my laptop is a convenience script-- it takes
in a text list of groceries, and spits out a formatted list based on aisle
locations so I can get in and out of the grocery store faster. My laptop is
the only place I can use this. I've tried using multiple CGI examples, and
it always results in a "File Not Found" error. Not even sure how I can debug
it. I can have the server do a simple one-line of printing "Hello World" but
anything more complicated than that makes it implode.

The most frustrating thing is how flippantly experienced programmers say to
use Django for Python web apps because it's so simple to use. It took me a
good half-day to just install it, and unless I'm writing a sample code or if
I want to clone a newspaper service, I have absolutely no idea how I would
use it efficiently. I want to learn the basics before running off to learn a
new framework. I'm trying to find good resources so I can continue self
teaching, but everything I find seems to be tailored to two classes: the
complete newbie who doesn't know how to print a line, or an advanced
programmer who is using list comprehension within a recursion with multiple
modules.

In short, is there a "simple" method for putting python scripts onto a
server that I do not host myself? I've seen web2py and it looks like it
would be more my speed, but support is lacking and doesn't seem too
compatible with my host. I use Dreamhost, and they are very adaptable and
configurable, but so far I can't find an easy way to accomplish what I want.

Thanks for reading this far if you did! I welcome any suggestions
whatsoever.

Eric
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing scripts and apps for Internet consumption

2010-07-31 Thread David Hutto
On Sat, Jul 31, 2010 at 2:27 PM, Eric Hamiter  wrote:
> Not sure if this is the right place for this, since this is a tutor list,
> but I think it is because it involves learning Python and the application of
> knowledge.
>
> I've just started learning it as my initial programming language as of two
> months ago. I like to think I'm making steady progress, and I now understand
> the most rudimentary level of the basics. What I keep reading is how Python
> is most powerful on server side applications, in the cloud, so to speak. The
> portability of Python is also widely evangelized.
>
> Here's my problem with this so far-- I can write a basic script, have it
> take in data, rearrange it, and spit it back out. Following along in a book,
> I can write a basic GUI or game. It's all wine and roses on my Windows
> laptop, where I have everything configured just right, with all of the
> modules in place where they need to be.
>
> Moving this to a server or even another computer so far has been a seemingly
> impossible task. There's a lot of documentation for CGI scripting (which is
> now frowned upon, with every page recommending looking into wsgi), and there
> have been applications devoted to transforming scripts into Windows
> executables (py2exe, etc.) but it seems like this is much more confusing
> than need be, and I can't get them to work regardless. When I try and google
> for solutions, choosing any terms like "web" or "server" point me to massive
> framework solutions like Django or Pylons, which seem extraordinarily
> complex for what I want.
>
> Specific examples: I have a livewires/pygame GUI game I wrote along with
> folowing the book "Python Programming for the Absolute Beginner" and it
> works great on my laptop. I tried installing Python/pygame on a work
> computer and copying my scripts over, but it endlessly fails with errors so
> obtuse I can't troubleshoot. I'm not even sure if I have the correct modules
> installed here. Should they be under "Lib" or "libs" or "includes"?  Trying
> to use py2exe fails because I can't figure out how to include non-scripts in
> the final program, like .pngs or .jpgs. How would I even begin to put this
> on a server? I'm clueless.
>
> Another program I am using on my laptop is a convenience script-- it takes
> in a text list of groceries, and spits out a formatted list based on aisle
> locations so I can get in and out of the grocery store faster. My laptop is
> the only place I can use this. I've tried using multiple CGI examples, and
> it always results in a "File Not Found" error. Not even sure how I can debug
> it. I can have the server do a simple one-line of printing "Hello World" but
> anything more complicated than that makes it implode.
>
> The most frustrating thing is how flippantly experienced programmers say to
> use Django for Python web apps because it's so simple to use. It took me a
> good half-day to just install it, and unless I'm writing a sample code or if
> I want to clone a newspaper service, I have absolutely no idea how I would
> use it efficiently. I want to learn the basics before running off to learn a
> new framework. I'm trying to find good resources so I can continue self
> teaching, but everything I find seems to be tailored to two classes: the
> complete newbie who doesn't know how to print a line, or an advanced
> programmer who is using list comprehension within a recursion with multiple
> modules.
>
> In short, is there a "simple" method for putting python scripts onto a
> server that I do not host myself? I've seen web2py and it looks like it
> would be more my speed, but support is lacking and doesn't seem too
> compatible with my host. I use Dreamhost, and they are very adaptable and
> configurable, but so far I can't find an easy way to accomplish what I want.
>
> Thanks for reading this far if you did! I welcome any suggestions
> whatsoever.
>
> Eric
>
>
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>

Get a linux hosting account, and a web address, most linux hosting
comes with python, so practice in the 'cloud'.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Simple Python Program

2010-07-31 Thread bob gailer

On 7/31/2010 12:00 AM, Jason MacFiggen wrote:
Can anyone tell me how to fix the errors I am getting if possible? I'm 
quite new and so confused...
I could give lots of diagnostic advice and detail. There is so much 
wrong with this program.


I suggest you discard it, back up and start with the simplest possible 
statement of the problem which is:

print a number randomly chosen from (0,1,2)

Then write the simplest possible program to do this one time; no loops 
or functions.
The entire program could then be written in 2 lines of code. Run it, fix 
it if it does not work. Once you have success -


Add (one at a time) various features:
- roll dice to select winner
- player names
- repetition

Continue to avoid writing functions. They are not necessary for such a 
simple program.


The final product will have less than 15 statements.


also how do I display the winner under the displayInfo function?
import random

def main():
print

playerOne, playerTwo = inputNames(playerOne, PlayerTwo)

while endProgram == 'no':

endProgram == no
playerOne = 'NO NAME'
playerTwo = 'NO NAME'

winnerName = rollDice(p1number, p2number, playerOne, 
playerTwo, winnerName)

winnerName = displayInfo

endProgram = raw_input('Do you want to end program? (Enter yes 
or no): ')


def inputNames(playerOne, PlayerTwo):

p1name = raw_input("Enter your name.")
p2name = raw_input("Enter your name.")
return playerOne, playerTwo

def random(winnerName):

p1number = random.randint(1, 6)
p2number = random.randint(1, 6)

if p1number > p2number:
playerOne = winnerName
elif p1number < p2number:
playerTwo = winnerName
else p1number == p2number:
playerOne, playerTwo = winnerName

def displayInfo():

#how do I display winner?

main()


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
   



--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing scripts and apps for Internet consumption

2010-07-31 Thread David Hutto
On Sat, Jul 31, 2010 at 2:34 PM, David Hutto  wrote:
> On Sat, Jul 31, 2010 at 2:27 PM, Eric Hamiter  wrote:
>> Not sure if this is the right place for this, since this is a tutor list,
>> but I think it is because it involves learning Python and the application of
>> knowledge.
>>
>> I've just started learning it as my initial programming language as of two
>> months ago. I like to think I'm making steady progress, and I now understand
>> the most rudimentary level of the basics. What I keep reading is how Python
>> is most powerful on server side applications, in the cloud, so to speak. The
>> portability of Python is also widely evangelized.
>>
>> Here's my problem with this so far-- I can write a basic script, have it
>> take in data, rearrange it, and spit it back out. Following along in a book,
>> I can write a basic GUI or game. It's all wine and roses on my Windows
>> laptop, where I have everything configured just right, with all of the
>> modules in place where they need to be.
>>
>> Moving this to a server or even another computer so far has been a seemingly
>> impossible task. There's a lot of documentation for CGI scripting (which is
>> now frowned upon, with every page recommending looking into wsgi), and there
>> have been applications devoted to transforming scripts into Windows
>> executables (py2exe, etc.) but it seems like this is much more confusing
>> than need be, and I can't get them to work regardless. When I try and google
>> for solutions, choosing any terms like "web" or "server" point me to massive
>> framework solutions like Django or Pylons, which seem extraordinarily
>> complex for what I want.
>>
>> Specific examples: I have a livewires/pygame GUI game I wrote along with
>> folowing the book "Python Programming for the Absolute Beginner" and it
>> works great on my laptop. I tried installing Python/pygame on a work
>> computer and copying my scripts over, but it endlessly fails with errors so
>> obtuse I can't troubleshoot. I'm not even sure if I have the correct modules
>> installed here. Should they be under "Lib" or "libs" or "includes"?  Trying
>> to use py2exe fails because I can't figure out how to include non-scripts in
>> the final program, like .pngs or .jpgs. How would I even begin to put this
>> on a server? I'm clueless.
>>
>> Another program I am using on my laptop is a convenience script-- it takes
>> in a text list of groceries, and spits out a formatted list based on aisle
>> locations so I can get in and out of the grocery store faster. My laptop is
>> the only place I can use this. I've tried using multiple CGI examples, and
>> it always results in a "File Not Found" error. Not even sure how I can debug
>> it. I can have the server do a simple one-line of printing "Hello World" but
>> anything more complicated than that makes it implode.
>>
>> The most frustrating thing is how flippantly experienced programmers say to
>> use Django for Python web apps because it's so simple to use. It took me a
>> good half-day to just install it, and unless I'm writing a sample code or if
>> I want to clone a newspaper service, I have absolutely no idea how I would
>> use it efficiently. I want to learn the basics before running off to learn a
>> new framework. I'm trying to find good resources so I can continue self
>> teaching, but everything I find seems to be tailored to two classes: the
>> complete newbie who doesn't know how to print a line, or an advanced
>> programmer who is using list comprehension within a recursion with multiple
>> modules.
>>
>> In short, is there a "simple" method for putting python scripts onto a
>> server that I do not host myself? I've seen web2py and it looks like it
>> would be more my speed, but support is lacking and doesn't seem too
>> compatible with my host. I use Dreamhost, and they are very adaptable and
>> configurable, but so far I can't find an easy way to accomplish what I want.
>>
>> Thanks for reading this far if you did! I welcome any suggestions
>> whatsoever.
>>
>> Eric
>>
>>
>> ___
>> Tutor maillist  -  tu...@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>
> Get a linux hosting account, and a web address, most linux hosting
> comes with python, so practice in the 'cloud'.
>
 Some might argue that this would be a production server, so to speak,
but it's just for target practice, right?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Simple Python Program

2010-07-31 Thread David Hutto
On Sat, Jul 31, 2010 at 2:35 PM, bob gailer  wrote:
> On 7/31/2010 12:00 AM, Jason MacFiggen wrote:
>
> Can anyone tell me how to fix the errors I am getting if possible? I'm quite
> new and so confused...
>
>
> I could give lots of diagnostic advice and detail. There is so much wrong
> with this program.
>
> I suggest you discard it, back up and start with the simplest possible
> statement of the problem which is:
> print a number randomly chosen from (0,1,2)
>
> Then write the simplest possible program to do this one time; no loops or
> functions.
> The entire program could then be written in 2 lines of code. Run it, fix it
> if it does not work. Once you have success -
>
> Add (one at a time) various features:
> - roll dice to select winner
> - player names
> - repetition
>
> Continue to avoid writing functions. They are not necessary for such a
> simple program.

Not to butt in, but... As a newbie didn't you want to feel like a
sophisticated programmer and use functions to hold simple, and use
instances as your 'pride stance'.



>
> The final product will have less than 15 statements.
>
> also how do I display the winner under the displayInfo function?
>
> import random
>
> def main():
>     print
>
>     playerOne, playerTwo = inputNames(playerOne, PlayerTwo)
>
>     while endProgram == 'no':
>
>     endProgram == no
>     playerOne = 'NO NAME'
>     playerTwo = 'NO NAME'
>
>     winnerName = rollDice(p1number, p2number, playerOne, playerTwo,
> winnerName)
>     winnerName = displayInfo
>
>     endProgram = raw_input('Do you want to end program? (Enter yes or
> no): ')
>
> def inputNames(playerOne, PlayerTwo):
>
>     p1name = raw_input("Enter your name.")
>     p2name = raw_input("Enter your name.")
>     return playerOne, playerTwo
>
> def random(winnerName):
>
>     p1number = random.randint(1, 6)
>     p2number = random.randint(1, 6)
>
>     if p1number > p2number:
>     playerOne = winnerName
>     elif p1number < p2number:
>     playerTwo = winnerName
>     else p1number == p2number:
>     playerOne, playerTwo = winnerName
>
> def displayInfo():
>
>     #how do I display winner?
>
> main()
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
> --
> Bob Gailer
> 919-636-4239
> Chapel Hill NC
>
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing scripts and apps for Internet consumption

2010-07-31 Thread Eric Hamiter
>
> Get a linux hosting account, and a web address, most linux hosting
> comes with python, so practice in the 'cloud'.
>

I have that-- an account with Dreamhost. This hasn't solved my problems yet
though. Like I said, I can have it write a simple

Hello, World!

...but if I make it do anything more complex, I get a 404 error. To make my
question more simple-- how does one learn to create web apps with Python? It
seems to be that what it is advertised as, but not at all ready to go "out
of the box" for that type of thing. And that is fine, but I want to learn
how without having to learn a framework like Django-- yet. Or is this just
considered a kind of necessity?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing scripts and apps for Internet consumption

2010-07-31 Thread David Hutto
On Sat, Jul 31, 2010 at 2:45 PM, Eric Hamiter  wrote:
>> Get a linux hosting account, and a web address, most linux hosting
>> comes with python, so practice in the 'cloud'.
>
> I have that-- an account with Dreamhost. This hasn't solved my problems yet
> though. Like I said, I can have it write a simple
>
> Hello, World!
>
> ...but if I make it do anything more complex, I get a 404 error. To make my
> question more simple-- how does one learn to create web apps with Python? It
> seems to be that what it is advertised as, but not at all ready to go "out
> of the box" for that type of thing. And that is fine, but I want to learn
> how without having to learn a framework like Django-- yet. Or is this just
> considered a kind of necessity?
>

What do your server logs show the 404 error to be?
Debug it like a script. For me, it's the part about use your own
computer as a host(I'm working on that) that gets me, but a hosting
account is already set up, as opposed to using your own local host
setup, this is provided. So it's just using the html with python just
like with php. I'm not an expert at this, but I do have a little
experience with lamp, so python should just be a replacement of p in
lamp for pythin instead of php
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Making a sound

2010-07-31 Thread Michael Bernhard Arp Sørensen
Greetings, programs.

How do I create a sound from python? I'm thinking along the line of
generating a sinus wave where I can control the frequency and duration. I
want to produce a string of sounds based on a character string. Probably not
original. :-) I don't want to save a sound to a file and play it
afterwards. Preferably it should be something from PYPI. Any suggestions?

Thanks in advance.

Kind regards

Michael B. Arp Sørensen
Programmer / BOFH

"If you want to enter my network while I'm out, you can find my SSH-key
under my mouse mat" - Michael Bernhard Arp Sørensen
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing scripts and apps for Internet consumption

2010-07-31 Thread David Hutto
On Sat, Jul 31, 2010 at 2:56 PM, David Hutto  wrote:
> On Sat, Jul 31, 2010 at 2:45 PM, Eric Hamiter  wrote:
>>> Get a linux hosting account, and a web address, most linux hosting
>>> comes with python, so practice in the 'cloud'.
>>
>> I have that-- an account with Dreamhost. This hasn't solved my problems yet
>> though. Like I said, I can have it write a simple
>>
>> Hello, World!
>>
>> ...but if I make it do anything more complex, I get a 404 error. To make my
>> question more simple-- how does one learn to create web apps with Python? It
>> seems to be that what it is advertised as, but not at all ready to go "out
>> of the box" for that type of thing. And that is fine, but I want to learn
>> how without having to learn a framework like Django-- yet. Or is this just
>> considered a kind of necessity?
>>
>
> What do your server logs show the 404 error to be?
> Debug it like a script. For me, it's the part about use your own
> computer as a host(I'm working on that) that gets me, but a hosting
> account is already set up, as opposed to using your own local host
> setup, this is provided. So it's just using the html with python just
> like with php. I'm not an expert at this, but I do have a little
> experience with lamp, so python should just be a replacement of p in
> lamp for pythin instead of php
>

In other words, make it easy on yourself in the beginning, to avoid
frustration(and therefore a deterence of self toward your initial
language), and then learn the specifics in your downtime.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Making a sound

2010-07-31 Thread Luke Paireepinart
You can generate the sound data in a buffer either in native python or using 
numpy, then play it back by loading the buffer into a sound object with pygame. 
It depends how much control you want on the sounds. You might want to use 
csound or supercollider or something if you want to programmatically control a 
synth rather than directly creating the sound data.

Sent from my iPhone

On Jul 31, 2010, at 1:49 PM, Michael Bernhard Arp Sørensen 
 wrote:

> Greetings, programs.
> 
> How do I create a sound from python? I'm thinking along the line of 
> generating a sinus wave where I can control the frequency and duration. I 
> want to produce a string of sounds based on a character string. Probably not 
> original. :-) I don't want to save a sound to a file and play it afterwards. 
> Preferably it should be something from PYPI. Any suggestions?
> 
> Thanks in advance.
> 
> Kind regards
> 
> Michael B. Arp Sørensen
> Programmer / BOFH
> 
> "If you want to enter my network while I'm out, you can find my SSH-key under 
> my mouse mat" - Michael Bernhard Arp Sørensen
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Order Of Operations Question

2010-07-31 Thread Roel Schroeven
Op 2010-07-28 11:41, David Hutto schreef:
> From a practice exercise in Building Skills In Python page 64 I'm
> working on How Much Does The Atmosphere Weigh? Part 1:
> To check it states that the answer should be app. 10**18kg However,
> and I've checked to make sure that the math I've layed out matches up
> with the texts, I get 5.07360705863e+20

Are looking for the _weight_ or the _mass_? They're the same in everyday
usage, but not in physical contexts. As far as I can see, you're
calculating the mass, not the weight. Does the book mention the units?
If it's kg, it's mass they mean, if it's N, it's weight.

In case they're really after the weight, your calculations get simpler:

import math
def atmosphereWeight():
# Air pressure at sea level
P0 = 1.01325e5
# Approx. radius of earth
R = 6.37e6
# Approx. surface area of earth
A = 4 * math.pi * R**2
# Weight of atmosphere [units: N/m**2 * m**2 = N]
Wa = P0 * A
return Wa

Using that I get approx. 5.17e19, which is about 50 times larger than
the 1e18 value in the book. I guess my guess was incorrect, and they're
really after the mass (which, according to my calculations, is 5.17e19 /
9.81 = 5.27e18; 'only' about 5 times larger than the value in the book).

-- 
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
  -- Isaac Asimov

Roel Schroeven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing scripts and apps for Internet consumption

2010-07-31 Thread bob gailer

On 7/31/2010 2:45 PM, Eric Hamiter wrote:


Get a linux hosting account, and a web address, most linux hosting
comes with python, so practice in the 'cloud'.


I have that-- an account with Dreamhost. This hasn't solved my 
problems yet though. Like I said, I can have it write a simple


Hello, World!


Please post that code, and the URL you use to invoke it.


...but if I make it do anything more complex, I get a 404 error. 


Please post that code, and the URL you use to invoke it.

Do you import cgi?

There is a companion module (cgitb) that captures exceptions and returns 
the traceback to the web browser.


--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing scripts and apps for Internet consumption

2010-07-31 Thread Eric Hamiter
On Sat, Jul 31, 2010 at 4:48 PM, bob gailer  wrote:

>
>  Please post that code, and the URL you use to invoke it.
>

test.py: this works on my laptop but not on the server

http://pastebin.com/ChjUzLRU

test-working.py: this works on both laptop & server

http://pastebin.com/iLNTrGdW

both available for execution here:

http://erichamiter.com/xkred27/



> Do you import cgi?
>

Yes.


>
> There is a companion module (cgitb) that captures exceptions and returns
> the traceback to the web browser.
>

I tried that as well. I'm sure I'm missing something fundamental here, but
that's kind of obvious since I posted I don't know what I'm doing in the
first place! :)

Thanks,

Eric
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing scripts and apps for Internet consumption

2010-07-31 Thread Alan Gauld


"Eric Hamiter"  wrote

the most rudimentary level of the basics. What I keep reading is how 
Python
is most powerful on server side applications, in the cloud, so to 
speak. The

portability of Python is also widely evangelized.


I'm not sure I'd agree with that assertion. Python is a geeral purpose 
programming
language and is equally at home on  the desktop as on a server. Where 
it is
definitely not at home is inside a web client - that territory lies 
with Javascript

and Flash etc.

Here's my problem with this so far-- I can write a basic script, 
have it
take in data, rearrange it, and spit it back out. Following along in 
a book,
I can write a basic GUI or game. It's all wine and roses on my 
Windows
laptop, where I have everything configured just right, with all of 
the

modules in place where they need to be.


Good thats as it should be.

Moving this to a server or even another computer so far has been a 
seemingly

impossible task.


Moving desktop applications to a server is a non trivial task in anty 
language,
the paradigms are completely different and will usually require 
extensive
re-engineering of your designs. The most important aspect being that 
server
applications don't generally have a user interface - that goes on the 
client...



There's a lot of documentation for CGI scripting (which is
now frowned upon, with every page recommending looking into wsgi),


Yes although CGI is fine for very simple apps, but ay7thing more than 
trivial
really is better done on a web framework - but the frameworks all have 
a
steep learning curve. A bit like learning a new GUI framework - the 
principles

are all the same but the details are different..


have been applications devoted to transforming scripts into Windows
executables (py2exe, etc.) but it seems like this is much more 
confusing

than need be, and I can't get them to work regardless.


This is a fairly complex task and personally I prefer to just install 
Python
on the PC. After all thats what these packaging tools do under the 
covers,
you just get a copy of python for every application instead of a 
single

shared version... The tools make it as easy as they can but its never
going to be a trivial task.

for solutions, choosing any terms like "web" or "server" point me to 
massive

framework solutions like Django or Pylons,


These are not massive although they are non trivial.
Massive is something like Zope or Plone...
But they do make web programming much much easier than basic CGI.
And the more complex the application the more useful they become.



which seem extraordinarily complex for what I want.


Have you considered that what you want might be more complex than
you suspect? Web programming is more complex than desktop
programming and combines elements of GUI design, network programming
and server programming. To do it well requires a fauirly high level of 
skill - although
there are plenty folk out there not doing it well as the number of 
slow and

badly designed (from a users point of view) web sites testifies!

Specific examples: I have a livewires/pygame GUI game I wrote along 
with
folowing the book "Python Programming for the Absolute Beginner" and 
it

works great on my laptop. I tried installing Python/pygame on a work
computer and copying my scripts over, but it endlessly fails with 
errors so

obtuse I can't troubleshoot.


OK, providing you are just rubnning the game on the work PC in the 
same
way that you do on the laptop it should be fairly straightforward, 
assuming:

1) Are they the same versions of Python/GUI?
2) Arte they installed in the same place on the two machines?
3) Are the environment variables set to the same values?

If all of these is YES then we need some specific info about the 
errors.



I'm not even sure if I have the correct modules
installed here. Should they be under "Lib" or "libs" or "includes"?


If the modules are stangard(PyGame etc) then you shouldn't need to
worry about that the installers will have done that for you. You only
need to put your code in a place that Python can find it.

to use py2exe fails because I can't figure out how to include 
non-scripts in

the final program, like .pngs or .jpgs.


Need to ask a py2exe persopn for that, but I'd get the non exe version
working first that should be esier!


How would I even begin to put this on a server? I'm clueless.


If its a game you need to separate out the game logic, rules etc
from the GUI elelemts. The GUII elements then become a separate
client program that communicates with the server via some kind
of networking protocol. This is what I meant about re-engineering
your app for a server.

Another program I am using on my laptop is a convenience script-- it 
takes
in a text list of groceries, and spits out a formatted list based on 
aisle
locations so I can get in and out of the grocery store faster. My 
laptop is
the only place I can use this. I've tried using multiple CGI 
examples, and
it always results in a "File N

Re: [Tutor] Python - RPG Combat System

2010-07-31 Thread Alan Gauld


"Jason MacFiggen"  wrote


and also, what can I do instead of writing print so many times?


Learn about format strings and use triple quoted strings.



   else:
   print "Menu Selections: "
   print "1 - Attack"
   print "2 - Defend"
   print


menu1 = """
Menu Selections
1 - Attack
2 - Defend
"""

else:
   print menu1


   if choice == 1:
   mo_hp = mo_hp - my_dmg
   print "The Lich King is at ", mo_hp, "Hit Points"
   print "You did ", my_dmg, "damage!"
   print


   if choice == 1:
   mo_hp = mo_hp - my_dmg
   print "The Lich King is at %s Hit Points\nYou did %s 
damage\n" % (mo_hp, my_dmg)


HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Simple Python Program

2010-07-31 Thread Steven D'Aprano
On Sun, 1 Aug 2010 04:35:03 am bob gailer wrote:
> Continue to avoid writing functions. They are not necessary for such
> a simple program.

That is *terrible* advice. Absolutely awful.

Functions should not be avoided unless necessary. Functions should be 
used unless there is a compelling reason to avoid them.

This is not 1972 any more, and we're not teaching kids to write 
spaghetti code with BASIC and GOTO. Functions, or their object-oriented 
equivalent methods, are *the* single most important feature of 
programming. And they're simple too -- they might not be the first 
thing you teach an absolute newbie who has never programmed before, but 
they're pretty close.



-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Simple Python Program

2010-07-31 Thread Steven D'Aprano
Hi Jason, 

Let's see if we can clean up some of your program. You had this 
function:

> def inputNames(playerOne, PlayerTwo):
> p1name = raw_input("Enter your name.")
> p2name = raw_input("Enter your name.")
> return playerOne, playerTwo

Let's write this in plain English.

(1) The function takes as input a value called "playerOne", and a value 
called "PlayerTwo". Take careful note of the case (upper and lower) of 
the names, because that is important to Python.

(2) It asks the user for a name, and assigns it to the 
variable "p1name".

(3) It asks the user for a second name, and assigns it to "p2name".

(4) It then ignores p1name and p2name, and does nothing with them.

(5) And returns the variable playerOne and playerTwo. Take careful note 
that the *second* one does not match in case: it starts with a 
lowercase p, so you will get an error because "playerTwo" doesn't 
exist.


Let's re-write it to work. For starters, there's no need to pass the 
players' names to the function which is supposed to get the players' 
names. Secondly, "inputNames" is such a dry, overly technical name. 
Let's make it simpler and friendlier:

def get_names():
playerOne = raw_input("Enter the first player's name: ")
playerTwo = raw_input("Enter the second player's name: ")
return playerOne, playerTwo


Now let's write a function to roll a single dice. Single die. Whatever.

import random  # We need Python's random number generator.

def dice(sides=6):
return random.randint(1, sides)

This uses a *default value* for the number of sides. If you want to roll 
a ten-sided die, you call:

dice(10)

and it will give back a random number between 1 and 10. But if you want 
to roll the default six-sided die, you can leave out the 6 and just 
write:

dice()

and it will give back a random number between 1 and 10.

Let's write a small program to play dice between two players. We'll make 
a simple dice game function that returns the name of the winning 
player, or the empty string in the case of a draw.

def dice_game(playerOne, playerTwo):
 # playerOne rolls a single six-sided die.
rollOne = dice()
 # So does playerTwo.
rollTwo = dice()
# The winner is the one who gets a higher number.
if rollOne > rollTwo:
return playerOne
elif rollTwo > rollOne:
return playerTwo
else:
# They must have rolled equal, so a draw.
return ""


Putting it all together, copy the lines between the ## into a new 
file and save it as a Python program:

##

import random

def dice(sides=6):
return random.randint(1, sides)

def get_names():
playerOne = raw_input("Enter the first player's name: ")
playerTwo = raw_input("Enter the second player's name: ")
return playerOne, playerTwo

def dice_game(playerOne, playerTwo):
 # playerOne rolls a single six-sided die.
rollOne = dice()
 # So does playerTwo.
rollTwo = dice()
# The winner is the one who gets a higher number.
if rollOne > rollTwo:
return playerOne
elif rollTwo > rollOne:
return playerTwo
else:
# They must have rolled equal, so a draw.
return ""

def displayWinner(winner):
print "And the winner is: ",
if winner == "":
print "... the game is a draw!"
else:
print winner
print


def main():
playerOne, playerTwo = get_names()
play_again = True
while play_again:
print "Rolling dice..."
winner = dice_game(playerOne, playerTwo)
displayWinner(winner)
answer = raw_input("Would you like to play again? (y/n) ")
play_again = answer.strip().lower() in ('y', 'yes')

main()

##


Have a play around with that, and feel free to ask any questions you 
like.


-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Making a sound

2010-07-31 Thread Dave Angel

Michael Bernhard Arp S wrote:

Greetings, programs.

How do I create a sound from python? I'm thinking along the line of
generating a sinus wave where I can control the frequency and duration. I
want to produce a string of sounds based on a character string. Probably not
original. :-) I don't want to save a sound to a file and play it
afterwards. Preferably it should be something from PYPI. Any suggestions?

Thanks in advance.

Kind regards

Michael B. Arp Sørensen
Programmer / BOFH

"If you want to enter my network while I'm out, you can find my SSH-key
under my mouse mat" - Michael Bernhard Arp Sørensen

  
What's your platform?  If you're on Windows, consider the Win32 
extensions, or just get the Active Python distribution from ActiveState, 
which includes them, and other stuff for Windows.


Anyway, then look for the winsound.PlaySound() function.

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing scripts and apps for Internet consumption

2010-07-31 Thread Dave Angel

Eric Hamiter wrote:

On Sat, Jul 31, 2010 at 4:48 PM, bob gailer  wrote:

  

 Please post that code, and the URL you use to invoke it.




test.py: this works on my laptop but not on the server

http://pastebin.com/ChjUzLRU

test-working.py: this works on both laptop & server

http://pastebin.com/iLNTrGdW

both available for execution here:

http://erichamiter.com/xkred27/

  

> Do you import cgi?
>


Yes.
  
Not in this code, you don't.  You also don't need it for this case.  But 
I wonder if you're describing something different than what you're posting.





I can't see where you attempt to produce even the simplest of html in the

http://pastebin.com/ChjUzLRU 


code.  So why do you declare it as text/html ?

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing scripts and apps for Internet consumption

2010-07-31 Thread bob gailer

On 7/31/2010 6:40 PM, Eric Hamiter wrote:
On Sat, Jul 31, 2010 at 4:48 PM, bob gailer > wrote:



Please post that code, and the URL you use to invoke it.


test.py: this works on my laptop but not on the server

http://pastebin.com/ChjUzLRU

test-working.py: this works on both laptop & server

http://pastebin.com/iLNTrGdW

both available for execution here:

http:// erichamiter.com/ 
xkred27/ 




Do you import cgi?


Yes.


There is a companion module (cgitb) that captures exceptions and
returns the traceback to the web browser.


I tried that as well. I'm sure I'm missing something fundamental here, 
but that's kind of obvious since I posted I don't know what I'm doing 
in the first place! :)


Main difference I see is lack of any html tags in test.py! Try adding

  1.
 print ""
  2.
 print "Publix Aide"
  3.
 print ""
  4.
 # the store map print statements go here
  5.
 print ""

Where is erichamiter.com/  hosted? Is 
there a way I could put something there?


--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing scripts and apps for Internet consumption

2010-07-31 Thread Eric Hamiter
On Sat, Jul 31, 2010 at 9:53 PM, bob gailer  wrote:

>
> Main difference I see is lack of any html tags in test.py! Try adding
>
>1. print ""
> 2. print "Publix Aide"
> 3. print ""
> 4. # the store map print statements go here
> 5. print ""
>
> I understand that this would work-- my question was how can I get a python
script to "do anything" on it? Why is it returning a 404 error?


> Where is erichamiter.com/  hosted? Is
> there a way I could put something there?
>
>
A hosted server, so it's not on my local machine. Afraid I can't hand you
any access directly to it, I have too much personal stuff on there at the
moment. I would be more than happy to look at any code you would want to
send in an email or pastebin, and I could put it up for a test.

Thanks,

Eric
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Simple Python Program

2010-07-31 Thread bob gailer

On 7/31/2010 8:24 PM, Steven D'Aprano wrote:

On Sun, 1 Aug 2010 04:35:03 am bob gailer wrote:
   

Continue to avoid writing functions. They are not necessary for such
a simple program.
 

That is *terrible* advice. Absolutely awful.
   


Well I disagree. I was trying to steer the OP to get the simplest 
possible program running, then do incremental expansion.


In my understanding user defined functions serve these purposes:
1 - modularizing large programs
2 - factoring out common code
3 - providing callbacks
4 - choosing callable objects from some collection.
5 - providing a main function for the if __name__ == "__main__" idiom
6 - what else can we think of?

None of these are needed in this simple case.


Functions should not be avoided unless necessary. Functions should be
used unless there is a compelling reason to avoid them.
   


Once the OP has succeeded in getting some program running he will 
experience some relief and satisfaction. Then I think it is time to add 
language features. But not just for the sake of using them.

This is not 1972 any more, and we're not teaching kids to write
spaghetti code with BASIC and GOTO.


Functions have no relationship to avoiding spaghetti code. Python makes 
spaghetti code impossible since it lacks a goto or equivalent statement.



Functions, or their object-oriented
equivalent methods, are *the* single most important feature of
programming. And they're simple too


Well - almost simple. Until you get into issues like global vs local 
names, positional vs keyword arguments, required vs optional arguments, 
default values (especially mutable objects), * and **, generator 
functions, magic methods, nested defs, decorators, 



-- they might not be the first thing you teach an absolute newbie who has never 
programmed before


That is what our OP appears to be.

--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python - RPG Combat System

2010-07-31 Thread Steven D'Aprano
On Sun, 1 Aug 2010 10:20:31 am Alan Gauld wrote:
> "Jason MacFiggen"  wrote
>
> > and also, what can I do instead of writing print so many times?
>
> Learn about format strings and use triple quoted strings.

*slaps self in head*

Doh! How could I have forgotten triple quoted strings?

*slinks off in shame*


-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing scripts and apps for Internet consumption

2010-07-31 Thread bob gailer

On 7/31/2010 11:03 PM, Eric Hamiter wrote:


On Sat, Jul 31, 2010 at 9:53 PM, bob gailer > wrote:



Main difference I see is lack of any html tags in test.py! Try adding

  1.
  print ""
  2.
  print "Publix Aide"
  3.
  print ""
  4.
  # the store map print statements go here
  5.
  print ""

I understand that this would work--


Did you send the altered code to the server and test it? Did it succeed 
or fail? What happens if you put the grocery code into test-working.py?


my question was how can I get a python script to "do anything" on it? 
Why is it returning a 404 error?


Where is erichamiter.com/ 
hosted? Is there a way I could put something there?


A hosted server, so it's not on my local machine. Afraid I can't hand 
you any access directly to it, I have too much personal stuff on there 
at the moment. I would be more than happy to look at any code you 
would want to send in an email or pastebin, and I could put it up for 
a test.


Could I have a link to the hosting service? Perhaps I could set up my 
own account.



--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Simple Python Program

2010-07-31 Thread Luke Paireepinart
I think I agree more with bob on this one - he wasn't saying functions weren't 
important, just that they weren't important to this case. Sure polymorphism and 
inheritance and properties and generators and list comprehensions are awesome 
and useful, but I wouldn't suggest they are necessary for every situation.  
Same with Functions. That's just the trap that java fell into by making 
everything an object... It just adds a couple of useless lines that will just 
serve to confuse newbies and irritate pros.
IMHO.

Sent from my iPhone

On Jul 31, 2010, at 10:11 PM, bob gailer  wrote:

> On 7/31/2010 8:24 PM, Steven D'Aprano wrote:
>> On Sun, 1 Aug 2010 04:35:03 am bob gailer wrote:
>>   
>>> Continue to avoid writing functions. They are not necessary for such
>>> a simple program.
>>> 
>> That is *terrible* advice. Absolutely awful.
>>   
> 
> Well I disagree. I was trying to steer the OP to get the simplest possible 
> program running, then do incremental expansion.
> 
> In my understanding user defined functions serve these purposes:
> 1 - modularizing large programs
> 2 - factoring out common code
> 3 - providing callbacks
> 4 - choosing callable objects from some collection.
> 5 - providing a main function for the if __name__ == "__main__" idiom
> 6 - what else can we think of?
> 
> None of these are needed in this simple case.
> 
>> Functions should not be avoided unless necessary. Functions should be
>> used unless there is a compelling reason to avoid them.
>>   
> 
> Once the OP has succeeded in getting some program running he will experience 
> some relief and satisfaction. Then I think it is time to add language 
> features. But not just for the sake of using them.
>> This is not 1972 any more, and we're not teaching kids to write
>> spaghetti code with BASIC and GOTO.
> 
> Functions have no relationship to avoiding spaghetti code. Python makes 
> spaghetti code impossible since it lacks a goto or equivalent statement.
> 
>> Functions, or their object-oriented
>> equivalent methods, are *the* single most important feature of
>> programming. And they're simple too
> 
> Well - almost simple. Until you get into issues like global vs local names, 
> positional vs keyword arguments, required vs optional arguments, default 
> values (especially mutable objects), * and **, generator functions, magic 
> methods, nested defs, decorators, 
> 
>> -- they might not be the first thing you teach an absolute newbie who has 
>> never programmed before
> 
> That is what our OP appears to be.
> 
> -- 
> Bob Gailer
> 919-636-4239
> Chapel Hill NC
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Simple Python Program

2010-07-31 Thread Steven D'Aprano
On Sun, 1 Aug 2010 01:11:41 pm bob gailer wrote:
> On 7/31/2010 8:24 PM, Steven D'Aprano wrote:
> > On Sun, 1 Aug 2010 04:35:03 am bob gailer wrote:
> >> Continue to avoid writing functions. They are not necessary for
> >> such a simple program.
> >
> > That is *terrible* advice. Absolutely awful.
>
> Well I disagree. I was trying to steer the OP to get the simplest
> possible program running, then do incremental expansion.

In hindsight, my post was a bit harsh. I actually agreed with nearly 
everything else you said, which I should have said. So I would like to 
apologise for the way I said what I said.


> In my understanding user defined functions serve these purposes:
> 1 - modularizing large programs

Leave out the "large", and you would be right. There is no limit to how 
small a program need be before using functions becomes appropriate. A 
one line program could be better off split into functions if the line 
is complex enough.


> 2 - factoring out common code
> 3 - providing callbacks
> 4 - choosing callable objects from some collection.
> 5 - providing a main function for the if __name__ == "__main__" idiom
> 6 - what else can we think of?

Code reuse, ease of testing, simplicity of debugging, separating the 
interface from the implementation, avoiding side-effects, writing 
self-documenting code, ease of refactoring, simplifying redesign, and 
probably many others.


> None of these are needed in this simple case.

Whether needed or not, splitting the code into small, easily testable, 
self-contained pieces is never a bad thing to do. Sometimes, rarely, 
for performance reasons you might choose to give up all those positives 
in order to win a bit of extra performance in a critical section of 
code, but that's the only reason I can think of to *actively* avoid the 
use of functions. (As opposed to passively avoid them because you just 
started programming yesterday and haven't got to "Chapter 3: Functions" 
yet.)


> > Functions should not be avoided unless necessary. Functions should
> > be used unless there is a compelling reason to avoid them.
>
> Once the OP has succeeded in getting some program running he will
> experience some relief and satisfaction. Then I think it is time to
> add language features. But not just for the sake of using them.

This is an excellent point. I might take the OP many, many hours, or 
days, or weeks, of effort to get the entire program working. The 
process until that point is just frustration after frustration as 
things go wrong. Until the entire function is working, it's hard to see 
any positive progress because it's just bug after bug after bug.

Or he can write one function that does one small thing, debug that, and 
get a sense of satisfaction that this function, at least, is working as 
designed, before going on to write the next function. Functions are not 
just for the compiler, they are for the human reader and writer too. 
It's easier to wrap your brain around a small, self-contained piece of 
code that does one thing in three or five or ten lines, than a 
comparatively large piece of code that requires thirty or fifty.


> > This is not 1972 any more, and we're not teaching kids to write
> > spaghetti code with BASIC and GOTO.
>
> Functions have no relationship to avoiding spaghetti code. Python
> makes spaghetti code impossible since it lacks a goto or equivalent
> statement.

Nonsense. Procedural programming (the use of functions, procedures, or 
sub-routines) was created partly to avoid spaghetti code. The primary 
reason GOTO is to be avoided is because of the tendency to lead to 
spaghetti. (Technically a function call is a specialist kind of GOTO. 
In early BASIC, it would be like a GOSUB. But it is a structured jump, 
tamed and on a leash rather than running wild like GOTO.)

Of course, the use of functions in and of itself does not guarantee you 
won't write spaghetti code. Object oriented programming sadly lends 
itself to class-based spaghetti (fettuccine perhaps?). But that's not 
likely to be a risk here.


> > Functions, or their object-oriented
> > equivalent methods, are *the* single most important feature of
> > programming. And they're simple too
>
> Well - almost simple. Until you get into issues like global vs local
> names, positional vs keyword arguments, required vs optional
> arguments, default values (especially mutable objects), * and **,
> generator functions, magic methods, nested defs, decorators, 

Of course. But apart from the distinction between local and global, 
everything else is optional.


> > -- they might not be the first thing you teach an absolute newbie
> > who has never programmed before
>
> That is what our OP appears to be.

He's experienced enough to be attempting to write functions. I don't 
believe you are doing him a favour by discouraging him.



-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options: