Search code examples
loopspython-3.7integer-arithmetic

How to keep a value from resetting in a loop?


I have been trying to set up a health system for my school project but the value keeps resetting regardless of the fact that monster_health is outside of the loop, my friends and I can't seem to fix this.

Code:

monster_health = 100
while monster_health > 0:
    playeraction = input('Placeholder beast wants to fight you!!\n1) Basic Attack')
    print(monster_health)
    if playeraction == 1:
        monster_health = monster_health-7
        continue
    if monster_health <= 0:
        print('You killed the monster!! Gain nothing, this is a test you barbarian')
        break

Output:

Placeholder beast wants to fight you!!
1) Basic Attack
100 #should print 97 but fails to

Solution

  • replace

        if playeraction == 1:
    

    with

        if playeraction == "1":
    

    or:

        if int(playeraction) == 1:
    

    Because your input is string format and not an integer and 1 != "1".