1
def diserang(self, lawan, attackPower_lawan):
    print(self.name + " Diserang " + lawan.name)
    attack_diterima = attackPower_lawan / self.armorNumber
    print("Serangan terasa :", str(attack_diterima))
    self.health -= attack_diterima
    print("Darah " + self.name + " tersisa " + str(self.health))
    

# TODO make hero attack and armor status up by 30% if the health reach by 50%
def statusUp(self, attUp, armUp):
    if self.health <= 50 :
        attUp += 30
        armUp += 30 
    else:
        raise Exception("Darah Anda Masih Normal")

i want to call the "statusUp" inside the "diserang", can anyone help me and explain it to me? im new at programming, thanks!

7
  • 5
    You do it the same way you call a function anywhere else. Commented Feb 5, 2021 at 4:32
  • How attUp & armUp are used in your code? Commented Feb 5, 2021 at 4:34
  • 2
    Please repeat on topic and how to ask from the intro tour. “teach me this language feature” is not a Stack Overflow issue. We expect you to make an honest attempt, and then ask a specific question about your algorithm or technique. Stack Overflow is not intended to replace existing documentation and tutorials. Asking for a personal tutorial is well out of scope for this site. Commented Feb 5, 2021 at 4:40
  • self.statusUp(attUp,arrmUp) Commented Feb 5, 2021 at 4:45
  • hi @Prune yes i use that source code from youtube tutorial but i make some other function (statusUp) that i think by myself and i already attempt it many times by myself but i keep cant think about the logic, thats why im asking here. im sorry if i do something wrong,because im a newbie at stackoverflow and programing, Commented Feb 5, 2021 at 5:10

2 Answers 2

2

A function defined inside another function is called a nested function. Nested functions can access variables of the enclosing scope. In Python, these non-local variables are read-only by default and we must declare them explicitly as non-local (using nonlocal keyword) in order to modify them.

You use inner functions to protect them from everything happening outside of the function, meaning that they are hidden from the global scope.

Here’s a simple example that highlights that concept:

def outer(num1):
def inner_increment(num1):  # Hidden from outer code
    return num1 + 1
num2 = inner_increment(num1)
print(num1, num2)

inner_increment(10)
# outer(10)

Try calling inner_increment():

Traceback (most recent call last):
File "inner.py", line 7, in <module>
inner_increment()
NameError: name 'inner_increment' is not defined

Now comment out the inner_increment() call and uncomment the outer function call, outer(10), passing in 10 as the argument:

10 11

Note: Keep in mind that this is just an example. Although this code does achieve the desired result, it’s probably better to make inner_increment() a top-level “private” function using a leading underscore: _inner_increment().

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the explanation! appreciate it senpai
Delvin if you like my answer...accept it. thanks
2
# TODO make hero attack and armor status up by 30% if the health reach by 50%
def statusUp(self, attUp, armUp):
    if self.health <= 50 :
        attUp += 30
        armUp += 30 
    else:
        raise Exception("Darah Anda Masih Normal")

def diserang(self, lawan, attackPower_lawan):
    print(self.name + " Diserang " + lawan.name)
    attack_diterima = attackPower_lawan / self.armorNumber
    print("Serangan terasa :", str(attack_diterima))
    self.health -= attack_diterima
    print("Darah " + self.name + " tersisa " + str(self.health))

    #mention your variables 
    statusUp(attUp, armUp)
    

Just call it inside function

2 Comments

wouldn't you require a self.statusUp(attUp,armUp) since they are in the same class
@AdityaGupta i already attempt it by call self.statusUp(attUp,armUp) inside the "diserang" function,turn out so many errors haha i'll try to research it first and learn something in youtube,i think my knowledge doesn't reach that level yet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.