back with more issues

Joel Goldstick joel.goldstick at gmail.com
Mon Aug 12 00:21:03 EDT 2013


On Sun, Aug 11, 2013 at 11:33 PM, Kris Mesenbrink
<krismesenbrink at gmail.com> wrote:
> import random
>
> def player():
>     hp = 10
>     speed = 5
>     attack = random.randint(0,5)
       # add the following line to return attack value:
       return attack

>
> def monster ():
>     hp = 10
>     speed = 4
>
> def battle(player):
>     print ("a wild mosnter appered!")
>     print ("would you like to battle?")
>     answer = input()
>     if answer == ("yes"):
you don't need the parentheses around "yes"

>         return player(attack)

you can't do that above because you defined the function with no
parameters.  If you alter player to return attack  you can alter the
above line to:
           return player()

>     else:
>         print("nope")

Its a bad idea to have a function return something down one path (If
True), then return nothing down another path.
>
>
> 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?

I wrote some quick changes above to give you what you want.  But you
need to understand more about functions.  Your player function does
next to nothing.  It defines two variables to fixed values, then gets
a random number and returns it.  Can you try to explain what you think
each of your functions is doing?  Every line should serve a purpose,
or it shouldn't be in the function.

> --
> http://mail.python.org/mailman/listinfo/python-list



-- 
Joel Goldstick
http://joelgoldstick.com



More information about the Python-list mailing list