No return, No parameter function:
def f1():
print("Name:","David Smith")
print("Total Score:",278)
f1()
No return, With parameters:
def f2(name,score):
print("Name:",name)
print("Total Score:",score)
f2("David Smith",278)
f2("John Doe",290)
List as Parameter:
def f3(name,scoreList):
sum_score = 0
print("Name",name)
for i in scoreList:
sum_score += i
print("Total Score:",sum_score)
f3("David SMith",[98,88,90])
f3("John Doe",[77,79,95])
With return, With parameters:
def f5(name,score):
sum_score = 0
for i in score:
sum_score += i
print("Name:",name)
return sum_score
result = f5("David Smith",[89,92,70])
print(result)
Scope of Variables:
def f5(name,score):
global math#force to use global variable
math = math - 50
print(math)
sum_score = 0
for i in score:
sum_score += i
print(name)
return sum_score
#print(sum_score)#We can not use variable inside a function from outside of the function
name1 = input("Please input student name:")
chinese = int(input("Please input "+name1+"'s French score:"))
math = int(input("Please input "+name1+"'s Math score:"))
english = int(input("Please input "+name1+"'s English score:"))
result = f5(name1,[chinese,math,english])
if result >= 270:
print("Excellent.")
else:
print("Keep going.")
Homework:
def list_slice(list1):
list2 = []#create an empty list
for i in list1:
if i%2 != 0:
list2.append(i)
return list2
result = list_slice([3,9,6,18,28,33,45,80,103])
print(result)
[…] |____Unit 41 English-Chinese Dictionary| |____Unit 42 String Operations|____Python Level 5| |____Unit 43 Glance at Functions| |____Unit 44 Advanced functions| |____Unit 45 Use of functions| |____Unit 46 Object-oriented […]