Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52106

Re: new to While statements

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <joshua.landau.ws@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'elif': 0.05; '#if': 0.07; 'method.': 0.07; 'none:': 0.07; 'part,': 0.09; 'spaces': 0.09; 'whole,': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'random': 0.14; '"break"': 0.16; '"idle"': 0.16; 'brackets': 0.16; 'brackets.': 0.16; 'function?': 0.16; 'naming,': 0.16; 'redundant': 0.16; 'rigorous': 0.16; 'skip:{ 30': 0.16; 'spacing': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'options.': 0.19; 'meant': 0.20; 'machine': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; '"you': 0.24; 'cc:2**0': 0.24; 'options': 0.25; 'skip:" 20': 0.27; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'room': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'lines': 0.31; 'probably': 0.32; 'covered': 0.32; 'run': 0.32; 'says': 0.33; 'comment': 0.34; 'maybe': 0.34; "i'd": 0.34; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'consistent': 0.36; 'false': 0.36; 'next': 0.36; 'should': 0.36; 'level': 0.37; 'subject:new': 0.38; 'problems': 0.38; 'rather': 0.38; 'sure': 0.39; 'skip:p 20': 0.39; 'space': 0.40; 'august': 0.61; 'new': 0.61; 'times': 0.62; "you've": 0.63; 'soon': 0.63; 'choose': 0.64; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'here': 0.66; 'between': 0.67; 'yes': 0.68; 'anything.': 0.68; 'subject': 0.69; 'carefully': 0.74; 'appreciative': 0.84; 'layout.': 0.84; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type:content-transfer-encoding; bh=Pz0l/ih/KHeLosfMxdELhg1XsJOlqt9F4Wmqbgzdn1M=; b=u6vOJndxRaKd+SmTZdfHWZ98DQlzi4DdxLCJzw44QiLw/mhc3sD49tFpl7TIa+3qpF uOlxTYGTDUQeBkkCGcpqjqXDq2/v9vdWHwNuFNiMfIynBbaZGhUhm8JDumTsSLo/icRi V6XFZPThnsIbLKIfU6b79Oc5VRM6hZlOj79Eb8njlt4pgEshuLPE0KuKS1T9AOfmKzXc zPF21iKf0m1kMkNw/PykWbqimYSlA9DF6nfAymJVh8/0WA2CIdb2ESSKtTq6yGCd8c4W KXfD9d+cB2sWzQXAhrUYx64DithTFwrS0ZQnklfL0nI9ShsFDt3y25jBRXl09by0UmE6 jmPw==
X-Received by 10.112.130.134 with SMTP id oe6mr835966lbb.30.1375850299198; Tue, 06 Aug 2013 21:38:19 -0700 (PDT)
MIME-Version 1.0
Sender joshua.landau.ws@gmail.com
In-Reply-To <69a10630-98dc-4a31-89ef-2770b3c1fd70@googlegroups.com>
References <69a10630-98dc-4a31-89ef-2770b3c1fd70@googlegroups.com>
From Joshua Landau <joshua@landau.ws>
Date Wed, 7 Aug 2013 05:37:39 +0100
X-Google-Sender-Auth sCH3tTprit03mqNqS-WWT3PTlOk
Subject Re: new to While statements
To krismesenbrink@gmail.com
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python-list <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.295.1375850769.1251.python-list@python.org> (permalink)
Lines 176
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1375850769 news.xs4all.nl 15870 [2001:888:2000:d::a6]:41932
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52106

Show key headers only | View raw


On 7 August 2013 04:38,  <krismesenbrink@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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

new to While statements krismesenbrink@gmail.com - 2013-08-06 20:38 -0700
  Re: new to While statements snakeinmyboot <mikelhamer@gmail.com> - 2013-08-06 21:09 -0700
    Re: new to While statements krismesenbrink@gmail.com - 2013-08-06 21:13 -0700
  Re: new to While statements snakeinmyboot <mikelhamer@gmail.com> - 2013-08-06 21:17 -0700
  Re: new to While statements Joshua Landau <joshua@landau.ws> - 2013-08-07 05:37 +0100
  Re: new to While statements Vito De Tullio <vito.detullio@gmail.com> - 2013-08-07 07:20 +0200
  Re: new to While statements Dan Sommers <dan@tombstonezero.net> - 2013-08-07 05:35 +0000
    Re: new to While statements snakeinmyboot <mikelhamer@gmail.com> - 2013-08-06 22:42 -0700
      Re: new to While statements Dan Sommers <dan@tombstonezero.net> - 2013-08-07 05:52 +0000
        Re: new to While statements krismesenbrink@gmail.com - 2013-08-06 23:26 -0700
    Re: new to While statements Vito De Tullio <vito.detullio@gmail.com> - 2013-08-07 20:05 +0200
    Re: new to While statements Dave Angel <davea@davea.name> - 2013-08-08 01:31 +0000
  Re: new to While statements Larry Hudson <orgnut@yahoo.com> - 2013-08-07 20:21 -0700

csiph-web