[Tutor] Combat Error
Steven D'Aprano
steve at pearwood.info
Mon Jul 8 01:20:50 CEST 2013
On 08/07/13 08:40, Jack Little wrote:
> def lvl4combat():
> print "The barracks are constructed of wood and cobblestone."
> print "A large, single cannon sits on the top, searching the sky for enemy airships."
> print "The cannon is manned by a single team of three men."
> print "The cannon is large and industrial."
> print "Remember your training."
> cannonhealth=75
Here you start with cannonhealth set to 75. But you never lower the score, so it never changes and the loop doesn't end.
> cannondamage=random.choice([10,17,13])
> currhealth=health
> while cannonhealth > 0:
> print "They fired at you!"
> currhealth-cannonhealth
Here you calculate the number (currhealth - cannonhealth), which is the difference between your health and the cannon's health, but do nothing with the result. Which is probably a good thing, since the calculation doesn't mean anything.
Perhaps you meant this?
currhealth = currenthealth - cannondamage
or even this, which is another way of doing the same calculation:
currhealth -= cannondamage
> print "Your health is now",currhealth
> combat=raw_input(">>")
> if combat.lower()=="fire cannon 1":
> print "Fired!"
> cannonhealth-attack
Here you calculate the number (cannonhealth - attack), which is the difference between the cannon's health and the damage done, but do nothing with the result.
As I suggested the last time you made this same error, you might find it easier to see the problem if you put spaces around operators, so that they are a little easier to notice and read:
cannonhealth - attack
cannonhealth-attack
Like the above, you probably want:
cannonhealth = cannonhealth - attack
And you make a similar mistake in at least three other places. I will leave you to find them.
--
Steven
More information about the Tutor
mailing list