pythontext-based

Simple function not interning


I am writing a simple text-based game and have written a simple combat system. The system works like rock-paper-scissors. It will iterate the loop, nor with it even move pass the first printed line. Thanks!.

This is on Python3

I have tried making it an integer, string, if to elif and back, and many other things. Thanks!

def combat(ehealth,ename):
    while (ehealth > 0):
        playerhit=int(input("Would you like to [1] Stab, [2] Swipe, or [3] Bash?   "))
        comhit=random.uniform(1,3)
        if playerhit==1:
            if comhit==1:
                print("The", ename, "parryed the attack")
            elif comhit==2:
                print("The", ename, "blocked the attack")
            elif comhit==3:
                ehealth=ehealth-1
                print("You hit the", ename,"!")
        elif playerhit==2:
            if comhit==1:
                ehealth=ehealth-1
                print("You hit the", ename,"!")
            elif comhit==2:
                print ("The", ename, "parryed the attack")
            elif comhit==3:
                print("The", ename, "blocked the attack")
        elif playerhit==3:
            if comhit==1:
                print("The", ename, "blocked the attack")
            elif comhit==2:
                ehealth=ehealth-1
                print("You hit the", ename,"!")
            elif comhit==3:
                print("The", ename, "blocked the attack")

I expected the function to end the loop when "ehealth" reaches zero

It does not make it passed the first printed statement, as it does loop the input over and over again.

Thanks again, Steven


Solution

  • random.uniform(1,3)
    

    This is the issue. It returns a random float not an int. You are never reaching any of your nested if-statements.

    Try random.choice(range(1,4))

    Or better yet,

    random.randint(1, 3)