원문 : http://learnpythonthehardway.org/book/ex19.html
숫자형 매개변수 2개를 입력받아 인쇄하는 함수를 작성하고 테스트 해보자 (ex19.py)
def cheese_and_crackers(cheese_count, boxes_of_crackers):
print "You have %d cheeses!" % cheese_count
print "You have %d boxes of crackers!" % boxes_of_crackers
print "Man that's enough for a party!"
print "Get a blanket.\n"
print "We can just give the function numbers directly:"
cheese_and_crackers(20, 30)
print "OR, we can use variables from our script:"
amount_of_cheese = 10
amount_of_crackers = 50
cheese_and_crackers(amount_of_cheese, amount_of_crackers)
print "We can even do math inside too:"
cheese_and_crackers(10 + 20, 5 + 6)
print "And we can combine the two, variables and math:"
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)
결과
알 수 있는 내용
1. 매개변수로 변수, 그냥 데이터, 계산식 등을 넘길 수 있다.
2. 어떤 기능을 하는 함수를 작성해서 불러 쓰면 편리 하구나.
반응형
'Python > Python 어렵게 배우기' 카테고리의 다른 글
| Python 어렵게 배우기 - Exercise 21: Functions Can Return Something (0) | 2013.12.16 |
|---|---|
| Python 어렵게 배우기 - Exercise 20: Functions and Files (0) | 2013.12.09 |
| Python 어렵게 배우기 - Exercise 18: Names, Variables, Code, Functions (0) | 2013.12.05 |
| Python 어렵게 배우기 - Exercise 17: More Files (0) | 2013.12.04 |
| Python 어렵게 배우기 - Exercise 16: Reading and Writing Files (0) | 2013.12.03 |