Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need some help with loops
#1
i am trying to use while loops and if statements to make a text based battle sim in python but i cant figure out how to use the while loops with multiple variables
import random
T = 10
H = 10

while H >0 or T >0:
        print"its tylers turn"
        p2 = raw_input("use punch? ")

        if p2=="yes":
            punch2 = random.randint(2,4)
        H = H - punch2
        print "THE PUNCH LANDS! haydyns health is now"
        print(H)
else:
            print("what!?!?!")
            if H== H<1:
                print"-------------"
                print"TYLER WINS!!!"
                print"-------------"
                
                print"its haydyns turn"
            p = raw_input("use punch? ")

            if p=="yes":
                punch1 = random.randint(1,5)
            T = T - punch1
            print"THE PUNCH LANDS! tylers health is now"
            print(T)
else:
            print"what?!?!?"
        if T== T<1:
            print"--------------"
            print"HAYDYN WINS!!!"
            print"--------------"
when i go to run it it complains about how my else statements are invalid syntax but if i remove the else statements and try to run it the while loop doesn't include the second if then statement so its stuck being "tylers turn" also if i just get rid of the second turn and try to run it with the same while statement it doesn't end and wants both conditions to be met instead of just one of either one.
any way to fix this im also open for suggestion on how to make my code better
Reply
#2
You can only put an else after an if or an elif and for your script you should use a bool for the turn, if it's false haydin turn, true tyler.
Reply
#3
Check this:

import random

T = 10
H = 10

while H > 0 and T > 0:
    p2 = input("[Tylers] use punch? ")
    if p2 == "yes":
        H -= random.randint(1, 5)
        print("THE PUNCH LANDS! Haydyns health is now", H)
    else:
        print("What!?!?!")
    if H < 1:
        print("-------------")
        print("TYLER WINS!!!")
        print("-------------")
        break

    p = input("[Haydyns] use punch? ")
    if p == "yes":
        T -= random.randint(1, 5)
        print("THE PUNCH LANDS! Tylers health is now", T)
    else:
        print("What?!?!?")
    if T < 1:
        print("--------------")
        print("HAYDYN WINS!!!")
        print("--------------")
        break
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020