new to While statements

Joshua Landau joshua at landau.ws
Wed Aug 7 00:37:39 EDT 2013


On 7 August 2013 04:38,  <krismesenbrink at gmail.com> wrote:
> import random
>
>
>
> def room ():

No need for the space after "room".

>     hp = 10
>     while hp != 0:

"while hp:" would be idiomatic, but you probably want "while hp > 0"
if you allow negatives.

>         random_Number = random.randint(1, 2)

You meant "random_number", I'm sure ;).

>         #asking if you want to roll/play

Don't comment lines unless the comment says more than the line.

>         des = input("Would you like to roll the die?")
>
>         if des == ("y"):

You don't need the brackets. Try to avoid redundant brackets where
they don't clarify anything.

Additionally, you should probably be more lenient and consistent with
answers. Maybe something like:

    if des.casefold() in ("y", "yes"):

and its counterpart

    if des.casefold() in ("n", "no"):

>             print ("Rolling the die...")
>             print ("You rolled a...")
>             print (random_Number)

You should avoid spaces between the function and the brackets.

>             #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:

Think carefully here -- do you want to have a round when monster_hp is
greater *or equal* to 0? Maybe you would rather only if it's alive (hp
> 0)?

>                         print ("you attack")
>                         damage_done = random.randint(0,5)
>                         print ("You do ",damage_done,"damage")
>                         monster_hp = monster_hp - damage_done

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

Gah! You just kill him off? Are you sure you don't want to use a
"break" or "return" to quit the loop or function?

>             print ("Maybe next time!")
>         else:
>             print ("please enter yes or no")
>
> room()


As a whole, +1 for the good naming, decent attempt at spacing and a
mostly-sane layout.


> 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


PS: I'd use a state machine for times like this, eg.

    import random

    def prompt(action, retort_invalid=None):
        while "asking for reponse":
            response = input("Would you like to {}?".format(action)).casefold()

            if response in ["y", "yes"]:
                return True

            elif response in ["n", "no"]:
                return False

            else:
                if retort_invalid is not None:
                    print(retort_invalid)

    def room():
        state = "wandering"

        while "adventuring":

            if state == "idle":
                if prompt("wander around")
                    print("Rolling the die...")

                    roll = random.randint(1, 2)

                    print("You rolled a {}.".format(roll))

                    if roll == 1:
                        monster_hp = 10
                        print("Oh no! A Monster!")
                        state = "facing monster"

                    else:
                        print("You find nothing")

                else:
                    print("Maybe next time!")
                    return

            elif state == "facing monster":
                will_attack =

                if prompt("attack", "You and the monster just stare at
one another"):
                    state = "attacking"

                else:
                    print("you run away")
                    state = "idle"

            elif state == "attacking":
                damage_done = random.randint(0, 5)
                monster_hp -= damage_done

                print("You attack to do", damage_done, "damage")
                print("The monster takes a hit, it has", monster_hp, "hp left")

                if monster_hp <= 0:
                    state = "idle"

    room()


The advantage of this is everything sits at the same level so you know
whether you've covered all options and permutations of options. It's
only longer because I made the "prompt" function which is more
rigorous than the current method.



More information about the Python-list mailing list