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


Groups > comp.lang.python > #52406

Re: back with more issues

Path csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed0.kamp.net!newsfeed.kamp.net!feeder1.cambriumusenet.nl!feed.tweaknews.nl!85.12.40.139.MISMATCH!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'variables': 0.07; 'attack.': 0.09; 'attributes': 0.09; 'defines': 0.09; 'function,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'random': 0.14; 'assigns': 0.16; 'attributes.': 0.16; 'character.': 0.16; 'kris': 0.16; 'parameters,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'value.': 0.19; 'later': 0.20; 'import': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; 'guys': 0.24; 'player': 0.26; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'related': 0.29; 'code': 0.31; 'names.': 0.31; 'class': 0.32; 'probably': 0.32; 'run': 0.32; 'another': 0.32; 'sense': 0.34; 'skip:_ 10': 0.34; 'could': 0.34; 'subject:with': 0.35; 'but': 0.35; 'skip:+ 70': 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'wrong': 0.37; 'two': 0.37; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'how': 0.40; 'subject:back': 0.60; 'lost': 0.61; 'refer': 0.63; 'stand': 0.64; 'subject:more': 0.64; 'incorporate': 0.68; 'battle,': 0.84; 'battle.': 0.84; 'confusing': 0.84; 'returns.': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dave Angel <davea@davea.name>
Subject Re: back with more issues
Date Mon, 12 Aug 2013 05:54:38 +0000 (UTC)
References <cc701481-0ecd-411b-8473-0596b4252949@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 174.32.174.30
User-Agent XPN/1.2.6 (Street Spirit ; Linux)
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.493.1376286900.1251.python-list@python.org> (permalink)
Lines 77
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1376286900 news.xs4all.nl 15928 [2001:888:2000:d::a6]:40927
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52406

Show key headers only | View raw


Kris Mesenbrink wrote:

> import random
>
> def player():
>     hp = 10
>     speed = 5
>     attack = random.randint(0,5)
>
The net resut of this function is nothing.  It assigns values, then
they're lost when the function returns.  A function is the wrong way to
deal with these three names.

> def monster ():
>     hp = 10
>     speed = 4

Same here.

>
> def battle(player):

You probably want to have two parameters, player and monster

>     print ("a wild mosnter appered!")
>     print ("would you like to battle?")
>     answer = input()
>     if answer == ("yes"):
>         return player(attack)
>     else:
>         print("nope")

This function makes no sense to me.  A function should have three
well-defined pieces:  what are its parameters, what does it do, what are
its side-effects, and what does it return.  This function is confusing
on all of those.

>
>
> battle()
>
>
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> this was a variation on a code that you guys already had helped me with,in the long run i plan to incorporate them together but as it stand i don't know how to call a specific variable from one function (attack from player) to use in another function (battle). what i want is to be able to use the variables from both player and monster to use in battle. any idea's?

What you should want is a class for each type of character. At its
simplest, the class can be a storage place for related named
attributes.

You could make a class Player, which defines attributes called hp,
speed, and attack.  Then later on you can refer to one of those
attributes with  synatax like  james.attack

class Player:
    def __init__(self):
        self.hp = 10
        self.speed = 5
        self.attack = random.randint(0,5)

Now, you create a player by
james = Player()

and the monster by
behemoth = Monster()

and you pass them into the function battle, by

result = battle(james, behemoth)

Inside the function, you'd say  player.attack to see that random value. 
 And monster.speed to see behemoth's speed.


-- 
DaveA

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


Thread

back with more issues Kris Mesenbrink <krismesenbrink@gmail.com> - 2013-08-11 20:33 -0700
  Re: back with more issues Joel Goldstick <joel.goldstick@gmail.com> - 2013-08-12 00:21 -0400
    Re: back with more issues Kris Mesenbrink <krismesenbrink@gmail.com> - 2013-08-11 21:35 -0700
      Re: back with more issues Joel Goldstick <joel.goldstick@gmail.com> - 2013-08-12 00:51 -0400
  Re: back with more issues Dave Angel <davea@davea.name> - 2013-08-12 05:54 +0000
    Re: back with more issues Kris Mesenbrink <krismesenbrink@gmail.com> - 2013-08-11 23:31 -0700
      Re: back with more issues Kris Mesenbrink <krismesenbrink@gmail.com> - 2013-08-11 23:57 -0700
      Re: back with more issues Dave Angel <davea@davea.name> - 2013-08-12 07:11 +0000
    Re: back with more issues Rotwang <sg552@hotmail.co.uk> - 2013-08-12 15:56 +0100
      Re: back with more issues random832@fastmail.us - 2013-08-12 15:46 -0400
        Re: back with more issues Kris Mesenbrink <krismesenbrink@gmail.com> - 2013-08-12 20:13 -0700
          Re: back with more issues MRAB <python@mrabarnett.plus.com> - 2013-08-13 04:31 +0100
          Re: back with more issues Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-08-12 23:48 -0400
          Re: back with more issues Steven D'Aprano <steve@pearwood.info> - 2013-08-13 06:22 +0000

csiph-web