[Tutor] Combat Error :p:

Paradox paradox at pobox.com
Mon Jul 8 01:21:11 CEST 2013


On 07/08/2013 06:40 AM, Jack Little wrote:
> When the player of my game fire his/her cannon, it is supposed to get 
> rid of some of the enemy's health. It does not. I have no clue what 
> the error is, so I need help. Here is the Python Shell output:
>
> >>fire cannon 1
> Fired!
> Enemy health= 75
> They fired at you!
> Your health is now 55
Sounds like a fun game (I am a sucker for cannon fire).
>
> Here is the code:
>
>
> cannonhealth=75
> cannondamage=random.choice([10,17,13])
> currhealth=health
> while cannonhealth > 0:
> print "They fired at you!"
> currhealth-cannonhealth
> print "Your health is now",currhealth
> combat=raw_input(">>")
> if combat.lower()=="fire cannon 1":
>     print "Fired!"
>     cannonhealth-attack
>     print "Enemy health=",cannonhealth
Where is "attack" defined? It isn't in the loop you provided that I can 
see.  It almost seems like you want to make a random choice of damage 
from a list and then subtract that damage from the health of the cannon 
(don't understand that, why would the cannon do damage to itself?). 
Maybe you would want to think about renaming some of your variables and 
decide if "cannondamage" and "attack" are supposed to be the same thing.

An easy way to debug these sorts of problems yourself is to add print 
statements.  For instance printing out the value of "attack" just before 
the line where you print the "Enemy health" may be enlightening.

thomas


More information about the Tutor mailing list