[Tutor] Conflict with encoding in console view and file dump

2010-07-30 Thread Alex Baraibar
Hello, would you please look at the comments in the code and help me with an
answer?
Thanx.

# -*- coding: cp1252 -*-

def festivos():
fest = [ 'Any Nou:\t\t\t1 de enero',
 'Reis:\t\t\t\t6 de enero',
 'Festa del Treball:\t\t1 de mayo',
 'Sant Joan:\t\t\t24 de junio',
 u'La Assumpció:\t\t\t15 de agosto',
 'La Diada:\t\t\t11 de septiembre',
 u'La Mercè:\t\t\t24 de septiembre',
 'La Hispanitat:\t\t\t12 de octubre',
 'Tots Sants:\t\t\t1 de novembre',
 u'La Constitució:\t\t\t6 de desembre',
 u'La Concepció:\t\t\t8 de desembre',
 'Nadal:\t\t\t\t25 de desembre',
 'Sant Esteve:\t\t\t26 de desembre' ]
return fest

def separador( num, char ):
return char * num

# --- Main ---
dias = festivos()
print "Los festivos fijos anuales son:\n"
for element in dias:
sep = separador( 50, '-' )

# If I turn off encoding latin-1, accented characters look
# wrong when I output them to a file from the command line, but
# if I turn on encoding, accented characters look
# wrong in the console view.
print element.encode( 'latin-1' )

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


Re: [Tutor] finding duplicates within a tuple of tuples

2010-07-30 Thread Peter Otten
Norman Khine wrote:

> Here is the latest version http://pastie.org/1066582 can this be
> further improved?

> # get all the duplicates and clean the products table
> main.execute("SELECT product_Id, url FROM %s.product WHERE url != ''" % 
db)
> results = main.fetchall()
> 
> d = defaultdict(set)
> for id, url in results:
>   d[url].add(id)
> 
> for ids in d.itervalues():
>   if len(ids) > 1:
>   # we now hove the first product_id added
>   canonical = min(ids)
>   ids = list(ids)
>   ids.pop(ids.index(canonical))
>   for id in ids:
>   update_product_id = 'UPDATE 
> oneproduct.productList_product_assoc SET 
productList_id=%s WHERE productList_id=%s'
>   main.execute(update_product_id, (id, canonical))
>   org.commit()
> main.close()

Yes; do it in SQL. Here's my attempt:

# Disclaimer: I stopped at the first point where it seemed to work; don't 
# apply the following on valuable data without extensive prior tests.
 
import sqlite3 
WIDTH = 80
db = sqlite3.connect(":memory:")

url_table = [
(24715,"http://aqoon.local/muesli/2-muesli-tropical-500g.html";),
(24719,"http://aqoon.local/muesli/2-muesli-tropical-500g.html";),
(24720,"http://example.com/index.html";)
]
cursor = db.cursor()
cursor.execute("create table alpha (id, url)")
cursor.executemany("insert into alpha values (?, ?)", url_table)

c2 = db.cursor()

id_table = [
(1, 24715),
(2, 24719),
(3, 24720)
]

cursor.execute("create table beta (id, alpha_id)")
cursor.executemany("insert into beta values (?, ?)", id_table)

def show(name):
print name.center(WIDTH, "-")
for row in cursor.execute("select * from %s" % name):
print row
print

print " BEFORE ".center(WIDTH, "=")
show("alpha")
show("beta")

cursor.execute("""
create view gamma as 
select min(a.id) new_id, b.id old_id from alpha a, alpha b 
where a.url = b.url group by a.url
""")
cursor.execute("""
update beta 
set alpha_id = (select new_id from gamma where alpha_id = old_id) 
where (select new_id from gamma where alpha_id = old_id) is not Null
""")

cursor.execute("""
delete from alpha 
where id not in (select min(b.id) 
from alpha b where alpha.url = b.url)
""")

print " AFTER ".center(WIDTH, "=")
show("alpha")
show("beta")

A database expert could probably simplify that a bit.

Again: duplicate records are best not created rather than removed. If you 
can create a unique index for the url column and alter your insertion code 
appropriately.

Peter

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


[Tutor] Python and Abaqus

2010-07-30 Thread leo.ruggier...@libero.it
Dear All,

I am trying to have access to the Abaqus kernel by a Python script. My 
intention is to avoid to use the Abaqus GUI (or command line) to run a 
simulation.
The idea is to lunch the Python script by DOS or Python module.

The problem is that my Python script runs from the Abaqus command line or from 
the GUI (menu-->file-->run script) but it doesn't run from outside.

Python doesn't recognize the Abqaus objects (i.e. mdb).

How could I solve this problem?

I need to automate the lunch of Abaqus simulations.

I hope someone could be able to help me.

Thanks a lot.


Regards,


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


Re: [Tutor] Conflict with encoding in console view and file dump

2010-07-30 Thread Peter Otten
Alex Baraibar wrote:

> Hello, would you please look at the comments in the code and help me with
> an answer?
> Thanx.
> 
> # -*- coding: cp1252 -*-
> 
> def festivos():
> fest = [ 'Any Nou:\t\t\t1 de enero',
>  'Reis:\t\t\t\t6 de enero',
>  'Festa del Treball:\t\t1 de mayo',
>  'Sant Joan:\t\t\t24 de junio',
>  u'La Assumpció:\t\t\t15 de agosto',
>  'La Diada:\t\t\t11 de septiembre',
>  u'La Mercè:\t\t\t24 de septiembre',
>  'La Hispanitat:\t\t\t12 de octubre',
>  'Tots Sants:\t\t\t1 de novembre',
>  u'La Constitució:\t\t\t6 de desembre',
>  u'La Concepció:\t\t\t8 de desembre',
>  'Nadal:\t\t\t\t25 de desembre',
>  'Sant Esteve:\t\t\t26 de desembre' ]
> return fest
> 
> def separador( num, char ):
> return char * num
> 
> # --- Main ---
> dias = festivos()
> print "Los festivos fijos anuales son:\n"
> for element in dias:
> sep = separador( 50, '-' )
> 
> # If I turn off encoding latin-1, accented characters look
> # wrong when I output them to a file from the command line, but
> # if I turn on encoding, accented characters look
> # wrong in the console view.
> print element.encode( 'latin-1' )
> 
> print sep
> raw_input()

Hm, I would expect an exception when you redirect the output to a file.
If you add

import sys
print >> sys.stderr, sys.stdout.encoding

what does the above print 
(a) when you print to the console
(b) when you redirect to a file?

Peter

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


[Tutor] problem with subprocess

2010-07-30 Thread Bala subramanian
Dear Friends,

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.

I login to the machine and run the script as 'python job.py'. But when i
logout from the machine, the job gets killed. So i submitted the job in
background as 'python job.py &'. Even in this case, when i logout from the
machine, the job gets killed. Why is this so. How can i avoid the same ?

#!/usr/bin/env python
from sys import argv
import subprocess
for line in open('job'):
subprocess.call(line,shell=True)

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


Re: [Tutor] Python and Abaqus

2010-07-30 Thread Knacktus

Am 30.07.2010 13:44, schrieb leo.ruggier...@libero.it:

Dear All,

I am trying to have access to the Abaqus kernel by a Python script. My
intention is to avoid to use the Abaqus GUI (or command line) to run a
simulation.
The idea is to lunch the Python script by DOS or Python module.

The problem is that my Python script runs from the Abaqus command line or from
the GUI (menu-->file-->run script) but it doesn't run from outside.


Without knowing very much about the Abaqus command line my guess is that 
a special environment (env. variables, path, etc.) is set up for the 
Abaqus command line. In that case you need to create that environment in 
your DOS command window where the python script is to run or create that 
environment within your python script (see the os module in the standard 
library).
But prior to that you need to know more about the environment. You could 
query the Abaqus command line (if it's a DOS window by invoking the 
python shell then using os.environ, if it's a Python you can use 
os.environ directly after importing os, of course).
Alternatively you could examine the command file, *.bat file or whatever 
opens the Abaqus command shell. Probably that file has a lot of set up 
stuff in it.


HTH and cheers,

Jan

___
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-30 Thread Hugo Arts
On Fri, Jul 30, 2010 at 2:36 PM, Bala subramanian
 wrote:
> Dear Friends,
>
> 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.
>

Why not just write a shellscript? that's essentially a list of jobs
anyway. if you make the first line of the file #! /bin/bash you can
basically execute it directly.

> I login to the machine and run the script as 'python job.py'. But when i
> logout from the machine, the job gets killed. So i submitted the job in
> background as 'python job.py &'. Even in this case, when i logout from the
> machine, the job gets killed. Why is this so. How can i avoid the same ?
>

you need to run 'nohup python job.py'. background process still get
SIGHUP when you log out, so they'll still exit.

http://www.computerhope.com/unix/unohup.htm

Hugo
___
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-30 Thread Bala subramanian
Thank you so much. I could see the job running with nohup after logout.

Bala

On Fri, Jul 30, 2010 at 3:49 PM, Hugo Arts  wrote:

> On Fri, Jul 30, 2010 at 2:36 PM, Bala subramanian
>  wrote:
> > Dear Friends,
> >
> > 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.
> >
>
> Why not just write a shellscript? that's essentially a list of jobs
> anyway. if you make the first line of the file #! /bin/bash you can
> basically execute it directly.
>
> > I login to the machine and run the script as 'python job.py'. But when i
> > logout from the machine, the job gets killed. So i submitted the job in
> > background as 'python job.py &'. Even in this case, when i logout from
> the
> > machine, the job gets killed. Why is this so. How can i avoid the same ?
> >
>
> you need to run 'nohup python job.py'. background process still get
> SIGHUP when you log out, so they'll still exit.
>
> http://www.computerhope.com/unix/unohup.htm
>
> Hugo
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python and Abaqus

2010-07-30 Thread bob gailer

On 7/30/2010 7:44 AM, leo.ruggier...@libero.it wrote:

Dear All,

I am trying to have access to the Abaqus kernel by a Python script. My
intention is to avoid to use the Abaqus GUI (or command line) to run a
simulation.
The idea is to lunch the Python script by DOS or Python module.
   

"lunch" mmm spam?

The problem is that my Python script runs from the Abaqus command line or from
the GUI (menu-->file-->run script) but it doesn't run from outside.

Python doesn't recognize the Abqaus objects (i.e. mdb).

How could I solve this problem?
   


Please learn to ask good questions. In this case specifically:

Tell us what you are doing to "run from outside". Be as specific and 
thorough as possible.


Tell us what results you are getting. Be as specific and thorough as 
possible.


"Doesn't run" and "doesn't recognize" are too vague.

Post your script if not too large else use a pastebin and provide the link.



--
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] A unicode print question

2010-07-30 Thread Joel Goldstick
I was reading this: http://diveintopython.org/xml_processing/unicode.html

and tried the exercise:

>>> s = u'La Pe\xf1a' [image: 1]
>>> print s   [image: 2]
Traceback (innermost last):
  File "", line 1, in ?
UnicodeError: ASCII encoding error: ordinal not in range(128)
>>> print s.encode('latin-1') [image: 3]
La Peña

 [image: 
1]

But oddly enough, when I typed it into my python shell I did NOT get the
UnicodeError, and I wonder why not:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = u'La Pe\xf1a'
>>> print s
La Peña
>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>


Maybe an earlier version of python produces the error, but not 2.6.5?
-- 
Joel Goldstick
___
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-30 Thread Richard D. Moores
On Thu, Jul 29, 2010 at 18:47, David Hutto  wrote:
> This is basically to get feedback, on a better way to show the
> greatest common divisor in fraction, in order to reduce it fully, than
> the one I've come up with. I'm sure there are better ways, so if you
> have simpler method, or critique of what I've done, let me know.

I've actually tried to run your script. If it works for you, then I'm
having a problem getting the indents correct. Could you put it up on
 or somewhere so it could be copied and
pasted accurately?

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


[Tutor] A unicode print question

2010-07-30 Thread Joel Goldstick
I was reading this: http://diveintopython.org/xml_processing/unicode.html

and tried the exercise:

>>> s = u'La Pe\xf1a'

>>> print s

Traceback (innermost last):
  File "", line 1, in ?
UnicodeError: ASCII encoding error: ordinal not in range(128)
>>> print s.encode('latin-1')

La Peña


But oddly enough, when I typed it into my python shell I did NOT get the
UnicodeError, and I wonder why not:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = u'La Pe\xf1a'
>>> print s
La Peña
>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>


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


Re: [Tutor] A unicode print question

2010-07-30 Thread Jerry Hill
On Fri, Jul 30, 2010 at 1:33 PM, Joel Goldstick wrote:

> I was reading this: http://diveintopython.org/xml_processing/unicode.html
>
> and tried the exercise:
>


> But oddly enough, when I typed it into my python shell I did NOT get the
> UnicodeError, and I wonder why not:
>

I believe that python is checking the encoding for stdout.  Try this:

print sys.stdout.encoding

When you attempt to print a unicode string, python will attempt to encode it
based on what it autodetected about your terminal.

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


Re: [Tutor] A unicode print question

2010-07-30 Thread Peter Otten
Joel Goldstick wrote:

> I was reading this: http://diveintopython.org/xml_processing/unicode.html
> 
> and tried the exercise:
> 
 s = u'La Pe\xf1a' [image: 1]
 print s   [image: 2]
> Traceback (innermost last):
>   File "", line 1, in ?
> UnicodeError: ASCII encoding error: ordinal not in range(128)
 print s.encode('latin-1') [image: 3]
> La Peña
> 
>  [image:
>  
[1]
> 
> But oddly enough, when I typed it into my python shell I did NOT get the
> UnicodeError, and I wonder why not:
> 
> Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
> [GCC 4.4.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
 s = u'La Pe\xf1a'
 print s
> La Peña
 import sys
 sys.getdefaultencoding()
> 'ascii'
>>
> 
> 
> Maybe an earlier version of python produces the error, but not 2.6.5?

This works a bit differently, probably since Python 2.3. See

http://docs.python.org/library/stdtypes.html#file.encoding

The "old" behaviour may still bite you when you redirect stdout.

$ python -c"print u'Pe\xf1a'"
Peña
$ python -c"print u'Pe\xf1a'" > /dev/null
Traceback (most recent call last):
  File "", line 1, in 
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 
2: ordinal not in range(128)

Peter

___
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-30 Thread David Hutto
On Fri, Jul 30, 2010 at 2:57 PM, Richard D. Moores  wrote:
> On Thu, Jul 29, 2010 at 18:47, David Hutto  wrote:
>> This is basically to get feedback, on a better way to show the
>> greatest common divisor in fraction, in order to reduce it fully, than
>> the one I've come up with. I'm sure there are better ways, so if you
>> have simpler method, or critique of what I've done, let me know.
>
> I've actually tried to run your script. If it works for you, then I'm
> having a problem getting the indents correct. Could you put it up on
>  or somewhere so it could be copied and
> pasted accurately?
>
> Dick Moores
>

This is the url:
http://python.pastebin.com/fP3jjqGj
___
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-30 Thread Richard D. Moores
On Fri, Jul 30, 2010 at 17:43, David Hutto  wrote:
> This is the url:
> http://python.pastebin.com/fP3jjqGj

This is a slight revision, with my suggested changes highlighted:


Dick Moores
___
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-30 Thread David Hutto
On Fri, Jul 30, 2010 at 10:25 PM, Richard D. Moores  wrote:
> On Fri, Jul 30, 2010 at 17:43, David Hutto  wrote:
>> This is the url:
>> http://python.pastebin.com/fP3jjqGj
>
> This is a slight revision, with my suggested changes highlighted:
> 
>
> Dick Moores
>
Before I  see it, here is my revised that shaved a whole 4 lines off
the original. I took out the comments before comparing, so the
pastebin count on the first is off by those comment lines.

http://python.pastebin.com/auw36h87
___
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-30 Thread David Hutto
On Fri, Jul 30, 2010 at 10:42 PM, David Hutto  wrote:
> On Fri, Jul 30, 2010 at 10:25 PM, Richard D. Moores  
> wrote:
>> On Fri, Jul 30, 2010 at 17:43, David Hutto  wrote:
>>> This is the url:
>>> http://python.pastebin.com/fP3jjqGj
>>
>> This is a slight revision, with my suggested changes highlighted:
>> 
>>
>> Dick Moores
>>
> Before I  see it, here is my revised that shaved a whole 4 lines off
> the original. I took out the comments before comparing, so the
> pastebin count on the first is off by those comment lines.
forgot the link:
http://python.pastebin.com/auw36h87
___
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-30 Thread David Hutto
On Fri, Jul 30, 2010 at 10:25 PM, Richard D. Moores  wrote:
> On Fri, Jul 30, 2010 at 17:43, David Hutto  wrote:
>> This is the url:
>> http://python.pastebin.com/fP3jjqGj
>
> This is a slight revision, with my suggested changes highlighted:
> 
>
> Dick Moores
>

http://python.pastebin.com/auw36h87
___
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-30 Thread David Hutto
On Fri, Jul 30, 2010 at 10:43 PM, David Hutto  wrote:
> On Fri, Jul 30, 2010 at 10:25 PM, Richard D. Moores  
> wrote:
>> On Fri, Jul 30, 2010 at 17:43, David Hutto  wrote:
>>> This is the url:
>>> http://python.pastebin.com/fP3jjqGj
>>
>> This is a slight revision, with my suggested changes highlighted:
>> 
>>
>> Dick Moores
>>
>
> http://python.pastebin.com/auw36h87
>

Maybe this time gmail will post the link, it keeps showing blank when I send::
http://pastebin.com/auw36h87
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python - RPG Combat System

2010-07-30 Thread Jason MacFiggen
I am have trouble figuring out how to make my program stop at 0 hit
points if I run it, it always goes into the negative hitpoints...

So my question is how do I make this program end at exactly 0 hit points
every time instead of going over?

and also, 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
___
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-30 Thread David Hutto
On Fri, Jul 30, 2010 at 10:44 PM, David Hutto  wrote:
> On Fri, Jul 30, 2010 at 10:43 PM, David Hutto  wrote:
>> On Fri, Jul 30, 2010 at 10:25 PM, Richard D. Moores  
>> wrote:
>>> On Fri, Jul 30, 2010 at 17:43, David Hutto  wrote:
 This is the url:
 http://python.pastebin.com/fP3jjqGj
>>>
>>> This is a slight revision, with my suggested changes highlighted:
>>> 
>>>
>>> Dick Moores
>>>
>>
>> http://python.pastebin.com/auw36h87
>>
>
> Maybe this time gmail will post the link, it keeps showing blank when I send::
> http://pastebin.com/auw36h87
>


This fixes the floating point 'bug' when numerator is greater than denominator:
http://python.pastebin.com/bJ5UzsBE
___
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-30 Thread David Hutto
On Fri, Jul 30, 2010 at 10:49 PM, Jason MacFiggen  wrote:
> I am have trouble figuring out how to make my program stop at 0 hit
> points if I run it, it always goes into the negative hitpoints...
>
> So my question is how do I make this program end at exactly 0 hit points
> every time instead of going over?
>
> and also, 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)

You ask for a random number

>     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

Then you your points by that random number, so if the random
subtracted from current hp is negative it will print that negative
when you ask it to print hp - mydmg.

So, you add another if hp <= 0, in addition to the if my mydmg>myhp,
then print 0, instead of hp - myd_mg

>     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
>
> ___
> 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] A better way for greatest common divisor

2010-07-30 Thread Steven D'Aprano
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*

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. 

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.

3. Your function has a serious bug. To see it, call gcd(5, 5) and see 
what it doesn't do.

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).



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


[Tutor] Simple Python Program

2010-07-30 Thread Jason MacFiggen
Can anyone tell me how to fix the errors I am getting if possible? I'm quite
new and so confused...

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


Re: [Tutor] Python - RPG Combat System

2010-07-30 Thread Steven D'Aprano
On Sat, 31 Jul 2010 12:49:36 pm Jason MacFiggen wrote:
> I am have trouble figuring out how to make my program stop at 0 hit
> points if I run it, it always goes into the negative hitpoints...
>
> So my question is how do I make this program end at exactly 0 hit
> points every time instead of going over?

I don't have much time to work on this now, but you should do something 
like:

while mo_hp > 0 or my_hp > 0:  
# combat stops when either combatant is dead
do_combat_round()
if mo_hp <= 0:
        print "The Lich King has been slain!"
    if my_hp <= 0:
        print "You have been slain by the Lich King!"


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

Don't write print so many times.

I don't understand the question. If you want to print 20 things, then 
you have to print 20 things. If that's too much printing, then only 
print 10 of them :)

I suppose you could lump two or three things together, in one call to 
print, by adding your own newlines into the strings. E.g. instead of

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

you could write:

print "Menu Selections: \n1 - Attack\n2 - Defend\n\n"

but I don't really see that as an advantage. Probably better to wrap 
common code into a function, then call the function:

def print_menu():
print "Menu Selections: "
print "1 - Attack"
print "2 - Defend"
print

print_menu()
choice = choose_from_menu()

etc.


-- 
Steven D'Aprano
___
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-30 Thread David Hutto
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
>
___
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-30 Thread David Hutto
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.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor