#전역변수 global : 함수 밖에서 정의한 변수 
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 = hp - 10 #10씩 hp가 줄어든다
    print('공격 받음!! 현재 체력:', hp)

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: