当我运行这段代码时,它只为 j=1 返回 d['a'] 我应该怎么做才能增加 j 的值?
def pattern():
d = {'a': ' | | ', 'b': '--- --- ---'}
j = 1
while j <= 11:
if j not in [4,8]:
return d['a']
else:
return d['b']
j+=1
答案 0 :(得分:0)
我看到您在每次执行外观时都试图一一获取模式。 一种替代方法是将所有结果放入一个数组中,然后返回该数组。
def pattern():
d = {'a': ' | | ', 'b': '--- --- ---'}
j = 1
result_pattern = []
while j <= 11:
if j not in [4,8]:
result_pattern.append(d['a'])
else:
result_pattern.append(d['b'])
j+=1
# return your array and loop over it after function call.
return result_pattern
你会像这样使用你的函数:
p = pattern()
for item in p:
# do something with your result.