#전역변수 golbal : 함수 밖에서 정의한 변수
name = '주현'

def hello():
    print(name + '아, 안녕!')

hello()
print(name)
#전역변수는 어디서든 사용할 수 있다


#지역변수 local : 함수 안에서 정의한 변수
def bye():
    animal = '강아지'
    print(animal + '야, 잘 가')

bye()
#print(animal)
#지역변수는 함수 안에서만 사용할 수 있다

x = 5 #전역변수
def add():
    global x #전역변수인 x를 사용하겠다
    x = 10
    y = 3 #지역변수
    print('x + y = ', x + y)

add()
print(x)
#print(y)

print('----문제----')
hp = 100
def damage():
    global hp
    hp = hp - 10
    print('공격 받음! 현재 체력 : ', hp)

damage()
damage()
damage()
print('최종 체력 : ', hp)

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: