-1

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

  • By *won't move past the first printed line*, do you mean it won't accept your input? Is there an exception raised? Also, what are your inputs for `ehealth` and `ename`? – C.Nivs Apr 23 '19 at 16:45
  • What did you initialize `ehealth` to? – Matt Cremeens Apr 23 '19 at 16:45
  • See this lovely [debug](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) blog for help. A couple of strategically-placed `print` statements would have solved this for you in a couple of minutes -- saving *your* time and stress. – Prune Apr 23 '19 at 16:53

1 Answers1

3
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)
rdas
  • 20,604
  • 6
  • 33
  • 46