如何减少执行相同操作的2个函数?
实际上,使用数组score
添加数组score2
是个问题。
def printPoint1(name, point, score):
for i in range(len(name)):
print("Name : " + name[i] + " Your score is of " + str(point[i]) + " : " + str(goal[i]) + " goals. ")
score.append(point[i])
def printPoint2(name, point, score2):
for i in range(len(name)):
print("Name : " + name[i] + " Your score is of " + str(point[i]) + " : " + str(goal[i]) + " goals. ")
score2.append(point[i])
答案 0 :(得分:1)
答案是不要创建两个完全相同的功能。只需创建一个功能,即可传递score
或score2
。
答案 1 :(得分:0)
一个功能的工作!不需要复制每个要处理的新变量的代码。 如果你定义:
def printPoint1(name, point, score):
for i in range(len(name)):
print("Name : " + name[i] + " Your score is of " + str(point[i]) + " : " + str(goal[i]) + " goals. ")
score.append(point[i])
然后您可以根据需要调用您的函数:
printPoint1(Var1, Var2, Var3)
使用任何不同的Var1
,Var2
,Var3
您应该尊重每个type
(int,float,list,string等)你运作的变量将会处理