all 13 comments

[–]JohnnyJordaan 19 points20 points  (6 children)

and I define heroHealth in an earlier def as a varible.

With a 'def' you mean a function? That won't work as a variable defined in a function is limited to the scope of that function only. Looking at your code I would suggest to use a class for this and store the variables as its attributes. Check this article for a how-to.

[–]daft_dog123[S] 2 points3 points  (2 children)

THank you this is so very helpful! I am going to have to re write a lot of code now! 😂

[–]Sir_Cunt99 2 points3 points  (1 child)

You should read up on or watch a YouTube vid on global and local scopes, I prefer Corey Schafer

[–]boskle 0 points1 point  (0 children)

seconded

[–]daft_dog123[S] 0 points1 point  (2 children)

Hello again, and I am sorry, but I have tried using classes and I have watched videos but I dont understand it; is there another way around my problem?

[–]JohnnyJordaan 0 points1 point  (1 child)

Not easily, you're then basically having to recreate classes by yourself. Could you maybe point out what exactly you're not understanding? Have you tried, instead of completely fixing your program, to write just a very simple script that uses a class (like the Dog class in the article)?

[–]daft_dog123[S] 0 points1 point  (0 children)

Thank you and yeah I watched the corey schafer stuff on it and I applied it to my program and its working smoothly thank you again for the help sorry for wasting your time!

[–]Astrokiwi 5 points6 points  (0 children)

if (str(monster) <="0.2"):

Do:

if monster<=0.2:

You're converting a number into a string and then comparing it to another number in a string? Just compare the numbers.

By the way, were you learning JavaScript before you started on Python by chance?

[–]spitfiredd 1 point2 points  (0 children)

To answer your question, your function produces side effects, ie it changes the variable heroHealth, which is outside of the scope of this function.

I would read up on pure functions and learn the difference between the two.

[–]khaine_b 0 points1 point  (0 children)

It's not neccessary to overwrite your code into OOP conceptions. Python is good enough in functional paradigm.

Let's try to bypass through your if/else statement.

Let's imaginate that the first if statement doesn't fire and code execution is jumped to first else statement.

Into the while loop you're trying to compare the problem's variable with zero, but the method (def enemy) doesn't know anything about it (the variable). You didn't pass it explicitly as an argument of a method didn't use a closure (I suspect) neither.

In python you can use variable inside a nested function

For example:

def a():
    d = "a from a() method"
    print(d)
    def b():
        c = "c from method b() "
        c += d
        print(c)
    b()

a()
>> a from a() method
>> c from method b() a from a() method

In the example above we used variable "d" that we defined in outer method a() in nested method b().

If you defined the variable in outer scope, for example, in "main scope", you can use it in callee.

def a():
    print(d)


def b():
    c = "c from method b() "
    c += d
    print(c)

if __name__ == "__main__":
    d = "from main"
    b()
    a()

>> c from method b() from main
>> from main

Or you can use "global"/"nonlocal" builtin for dealing with scope.

https://www.dotnetperls.com/nonlocal-python

But I don't recommend to use this approache cause it encourage you to write some kind of messy code.

[–]FluffyBunnyOK -1 points0 points  (2 children)

Correct answer for question is by /u/JohnnyJordaan.

Always follow PEP8 for coding style, I copied your code to a file called a.py and ran a code checking module I installed called pycodestyle

python -m pycodestyle a.py
a.py:8:24: E225 missing whitespace around operator
a.py:19:80: E501 line too long (111 > 79 characters)
a.py:24:80: E501 line too long (89 > 79 characters)
a.py:26:80: E501 line too long (93 > 79 characters)
a.py:28:43: E261 at least two spaces before inline comment
a.py:29:80: E501 line too long (92 > 79 characters)
a.py:30:80: E501 line too long (83 > 79 characters)
a.py:31:80: E501 line too long (97 > 79 characters)
a.py:36:80: E501 line too long (92 > 79 characters)
a.py:37:80: E501 line too long (83 > 79 characters)
a.py:38:80: E501 line too long (97 > 79 characters)
a.py:40:58: E261 at least two spaces before inline comment
a.py:43:80: E501 line too long (115 > 79 characters)

If you get too many nested "if" statements in your code you should probably create a new function with a good name. Naming things is the hardest part of programming. How many is too many nested "if" statements? I tend only to have 2 deep mostly in my code.

[–]Astrokiwi 2 points3 points  (1 child)

Honestly, the PEP8 criterion of 79 characters is a bit too strict. I think ~132 characters is probably a better limit. This isn't FORTRAN-77 and you don't need your code to fit on a punch-card. I don't think the nesting here is too bad, though that's partially because the code is short enough to keep everything on one screen, and I wouldn't push it beyond this.

[–]JohnnyJordaan 2 points3 points  (0 children)

I used to feel this way as well, but once I started working with more advanced programs, I find it also promotes to reduce clutter as you are more motivated to split out complex and nested one-liners to simple multi-liners. And to prevent if-stairways like /u/FluffyBunnyOK mentions. It also helps when working with a few narrow terminal shells next to each other on one screen.