Global Variable ์ฌ์ฉํ๊ธฐ
์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ๋ฅผ ํ๋ค๊ฐ ๋ฐ๊ฒฌํ.. function์์์ global variable์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
๋๋ ์ธ ์๊ฐ๋ ๋ชปํ๋๊ฑฐ๋ผ ํ๋ฒ ์ ๋ฆฌํด๋๊ณ ๋์ด๊ฐ๋ฉด ๋ค์์ ์ฐ๊ฒ ์ง ์ถ์ด์ ์ ๋ฆฌํด๋ณธ๋ค.
์ ์ญ๋ณ์๊ฐ ๋ฌด์์ธ์ง ์ด๋ป๊ฒ ์ฐ๋์ง๋ ์ ์๋ ์ฝ๋์ w3schools์ ๊ธ์ ์ฐธ๊ณ ํ๋ฉด ๋๋๊น
๋ฐ๋ก ์ ๋ฆฌํ์ง ์์๊ฑฐ๊ณ ์ด๋ป๊ฒ ๋ฌธ์ ์์ ์ผ๋์ง๋ง ๊ฐ๋ตํ ๊ธฐ๋กํด๋ณผ๋ ค๊ณ ํ๋ค.
def turn_left():
global direction
direction -= 1
if direction == -1:
direction = 3
- ๋ฐฉํฅ์ค์ ->์ด๋ ๋ฌธ์ ์ ํ์์ ๋ฐฉํฅ์ ํ ๊ธฐ๋ฅ ๊ตฌํ์ ์ํด ํจ์๋ฅผ ๋ง๋ฆ
- ๋ฐฉํฅ์ ๋ฐ๋ฅธ ์ด๋๋์ dx, dy๋ผ๋ ๋ ๊ฐ์ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค์ด์ ๊ด๋ฆฌํจ
- ๊ฐ direction์ ๋ฐ๋ผ dx,dy๋ฆฌ์คํธ์ ์ธ๋ฑ์ค๋ก ์ ๊ทผํ์ฌ ์ด๋ํ ๊ฒ
- direction์ ๋ณํ๋ ์ ํจ์๋ฅผ ํตํด ํ ๊ฒ์ด๊ณ , direction๋ณ์๋ฅผ ํตํด ๋ณํ๋์ ์ ๊ทผํ๋ ๊ฒ์ ์ ํจ์ ๋ฐ์์ ํ ๊ฑฐ๋๊น
- ํจ์ ์ํ์์ ๋ชจ๋ ์ธ ์ ์๊ฒ ํ๊ธฐ ์ํด global variable๋ก ์ ์ธํ์ฌ ์ฌ์ฉํ๋ค.
x = "awesome"
print("Python is " + x)
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
# Result
# Python is awesome
# Python is fantastic
https://www.w3schools.com/python/python_variables_global.asp
Python - Global Variables
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
www.w3schools.com
์ถ์ฒ: ์ด๊ฒ์ด ์ทจ์ ์ ์ํ ์ฝ๋ฉํ ์คํธ๋ค with ํ์ด์ฌ, w3schools
'๐ฌ Programming Language > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] str() ์ฌ์ฉํ๊ธฐ (0) | 2023.03.01 |
---|---|
Practice Tree Recursion (0) | 2022.07.08 |