new to While statements

krismesenbrink at gmail.com krismesenbrink at gmail.com
Tue Aug 6 23:38:24 EDT 2013


import random



def room ():

    hp = 10
    while hp != 0:

        random_Number = random.randint(1, 2)

        #asking if you want to roll/play
        des = input("Would you like to roll the die?")

        if des == ("y"):
            print ("Rolling the die...")
            print ("You rolled a...")
            print (random_Number)

            #a "monster" appers if you roll a 1""
            if random_Number == 1:
                monster_hp = 10
                print ("Oh no a Monsster!")
                print ("would you like to attack?")
                answer = input("y or n?")
                if answer == "y":
                    #if you choose to battle this is what happens
                    while monster_hp >=0:
                        print ("you attack")
                        damage_done = random.randint(0,5)
                        print ("You do ",damage_done,"damage")
                        monster_hp = monster_hp - damage_done
                        print ("the monster takes a hit, it has ", monster_hp,
                        "left")


                elif answer == ("n"):
                    print ("you run away")

                else:
                    print ("You and the monster just stare at one another")
            else:
                print ("You find nothing")
        # if you decisde to not play it will kill you
        elif des == ("no"):
            hp = 0
            print ("Maybe next time!")
        else:
            print ("please enter yes or no")

room()


this is the code i'm making. as the subject says im new to while statements. i am having problems with the monster battle part, it takes health away from the "monster" but as soon as it gets to 0 or less i'd like the code to start from the top and ask you to roll the die again. any help on this would be greatly appreciative



More information about the Python-list mailing list