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: 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 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 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On 7 August 2013 04:38, wrote: > import random > > > > def room (): No need for the space after "room". > hp =3D 10 > while hp !=3D 0: "while hp:" would be idiomatic, but you probably want "while hp > 0" if you allow negatives. > random_Number =3D 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 =3D input("Would you like to roll the die?") > > if des =3D=3D ("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 =3D=3D 1: > monster_hp =3D 10 > print ("Oh no a Monsster!") > print ("would you like to attack?") > answer =3D input("y or n?") > if answer =3D=3D "y": > #if you choose to battle this is what happens > while monster_hp >=3D0: 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 =3D random.randint(0,5) > print ("You do ",damage_done,"damage") > monster_hp =3D monster_hp - damage_done monster_hp -=3D damage_done > print ("the monster takes a hit, it has ", monste= r_hp, > "left") > > elif answer =3D=3D ("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 =3D=3D ("no"): > hp =3D 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 statemen= ts. 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 t= o start from the top and ask you to roll the die again. any help on this wo= uld be greatly appreciative PS: I'd use a state machine for times like this, eg. import random def prompt(action, retort_invalid=3DNone): while "asking for reponse": response =3D input("Would you like to {}?".format(action)).case= fold() 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 =3D "wandering" while "adventuring": if state =3D=3D "idle": if prompt("wander around") print("Rolling the die...") roll =3D random.randint(1, 2) print("You rolled a {}.".format(roll)) if roll =3D=3D 1: monster_hp =3D 10 print("Oh no! A Monster!") state =3D "facing monster" else: print("You find nothing") else: print("Maybe next time!") return elif state =3D=3D "facing monster": will_attack =3D if prompt("attack", "You and the monster just stare at one another"): state =3D "attacking" else: print("you run away") state =3D "idle" elif state =3D=3D "attacking": damage_done =3D random.randint(0, 5) monster_hp -=3D damage_done print("You attack to do", damage_done, "damage") print("The monster takes a hit, it has", monster_hp, "hp le= ft") if monster_hp <=3D 0: state =3D "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.