Python
Coursera_Python for everyone_Chapter 6.
심리밀당남
2016. 7. 30. 17:28
Week 6. Functions.
' 함수 정의하기. '
- syntax
def '함수명' (Parameter) :
~~~~
' Arguments '
- 함수에 대한 입력으로서 전달하는 값.
- 함수 호출할 때의 입력
' Parameter '
- A parameter is a variable which we use in the function definition that is a 'handle'
that allows the code in the function to access the arguments for a particular function invocation.
- 함수 정의에 사용.
' Return Values '
Assignment
4.6 Write a program to prompt the user for hours and rate per hour using raw_input to compute gross pay. Award time-and-a-half for the hourly rate for all hours worked above 40 hours. Put the logic to do the computation of time-and-a-half in a function called computepay() and use the function to do the computation. The function should return a value. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). You should useraw_input to read a string and float() to convert the string to a number. Do not worry about error checking the user input unless you want to - you can assume the user types numbers properly. Do not name your variable sum or use the sum() function.
def computepay(h, r):
if h > 40:
pay = 40 * r + (h - 40) * r * 1.5
else:
pay = h * r
return pay
try :
hrs = raw_input("Enter Hours:")
rate = raw_input("Enter Rate Per Hours : ")
h = float(hrs)
r = float(rate)
p = computepay(h, r)
print p
except :
print "please input numeric numbers"
if h > 40:
pay = 40 * r + (h - 40) * r * 1.5
else:
pay = h * r
return pay
try :
hrs = raw_input("Enter Hours:")
rate = raw_input("Enter Rate Per Hours : ")
h = float(hrs)
r = float(rate)
p = computepay(h, r)
print p
except :
print "please input numeric numbers"
후기
함수라는 개념에 대한 기초중에 기초라 딱히 새롭거나 어렵지는 않았다. Parameter와 Argument의 정의를 내가 이해한게 맞는지는 잘 모르겠다. 나중에 정확히 두 개념의 정의와 차이에 대해 알아보고 정리해야겠다.