我通过在线课程自学python,但我遇到了一个问题。
for loop
如何告诉python在for循环结束后做某事?
例如:
def mixer(g):
print(g)
def printer(h):
print("end")
X = input ("words : ")
var2 = ""
var1 = X.split()
for y in var1:
if len(y) > 5:
y.upper()
var2 += y.upper() + " "
mixer(var2)
elif len(y) <= 5:
y.lower()
var2 += y.lower() + " "
mixer(var2)
在完成循环之后,我想要做的是什么,我想在循环之后调用一个函数,你怎么能这样做? ##&gt;&gt;&gt;这种方法没有用
elif y == False:
print("false")
printer(var2)
答案 0 :(得分:0)
将它放在for循环体外:
for x in y:
... indented block does stuff with x on each loop ...
... dedented code (outside loop body) run once after loop is finished ...