Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why wont it stop

+2 votes
asked May 18, 2023 by localhosed3000 (140 points)
'''

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

'''
from random import randint
from time import sleep

'''
variables
'''
a = "choose what to do(1:attack, 2:heal, 3:shield)  "
choice = 0
bosssHp = 100
playerHp = 100
isShielding = False
attackDamage = 0
recievedDamage = 0
healedHp = 0
gameOn = True
'''
function
'''
def inform():
    print('the boss is at', bosssHp, 'hp')
    print('you are at', playerHp, 'hp')
    
    

    
'''
main
'''
print ('Text Boss Game')

while gameOn is True:
    if playerHp < 0:
        gameOn = False
    if bosssHp < 0:
        gameOn = False
    inform()
    choice = int(input(a))
    if choice == 1:
        attackDamage = randint(10,30)
        bosssHp -= attackDamage
        print('you dealt ', attackDamage, ' damage ')
        inform()
    elif choice == 2:
        healedHp = randint(5,150)
        playerHp += healedHp
        print('you healed ', healedHp, ' hp')
        inform()
    elif choice == 3:
        isShielding = True
        
    print('__________________________________')
    sleep(1)
    
    
    if isShielding is False:
        recievedDamage = randint(10,25)
        print('the boss dealt ', recievedDamage, ' damage')
        playerHp -= recievedDamage
    isShielding = False
    print('__________________________________')
    sleep(2)

if playerHp < 0:
    print('YOU LOSE')
if bosssHp < 0:
    print('YOU WIN')

1 Answer

0 votes
answered May 23, 2023 by Peter Minarik (88,520 points)

The issue is that you perform the exit check (player or boss HP drops below 0) before damage is even dealt. So if the game should be over because the boss died, it does not end, but a new round starts, and only then do you set the gameOn to False, but you've already started the next iteration.

One solution to this is to perform this check at the end of the loop.

I've also recommend doing the information printing just once in a loop, not multiple times.

And one more recommendation: HP == 0 should also count as death, which is not what you do right now (you check below 0 only).

Another issue I found is that if you check player death and boss death at the same time, then it is possible that the player has killed the boss, yet the dead boss still deals damage (potentially can kill the player and both of them could die).

A fix for this would be to check HP after damage is dealt separately for the boss and the player and break the loop when this happens. Otherwise, continue playing infinitely.

I've added these changes to your code below:

from random import randint
from time import sleep

'''
variables
'''
a = "choose what to do(1:attack, 2:heal, 3:shield)  "
choice = 0
bosssHp = 100
playerHp = 100
isShielding = False
'''
function
'''
def inform():
    print('the boss is at', bosssHp, 'hp')
    print('you are at', playerHp, 'hp')

'''
main
'''
print ('Text Boss Game')

while True:
    choice = int(input(a))
    if choice == 1:
        attackDamage = randint(10,30)
        bosssHp -= attackDamage
        print('you dealt ', attackDamage, ' damage ')
        if bosssHp <= 0:
            break
    elif choice == 2:
        healedHp = randint(5,150)
        playerHp += healedHp
        print('you healed ', healedHp, ' hp')
    elif choice == 3:
        isShielding = True
        
    print('__________________________________')
    sleep(1)
    
    if isShielding is False:
        recievedDamage = randint(10,25)
        print('the boss dealt ', recievedDamage, ' damage')
        playerHp -= recievedDamage
        if playerHp <= 0:
            break
    isShielding = False
    print('__________________________________')
    sleep(2)
    
    inform();

inform();
if playerHp < 0:
    print('YOU LOSE')
if bosssHp < 0:
    print('YOU WIN')
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...