我的for循环有for value1, value2 in args
失败了,我不知道为什么
def school_completion(*args):
"""
If any of the subjects have one or more incomplete testlet, the whole school is
incomplete.
:param args: tuple of 7 strs
Should come as:
(Eligible Students,
ELA Required,
ELA Completed,
Math Required,
Math Completed,
Science Required,
Science Completed)
:return: str
"""
# If there are no eligible students, return a dash.
if args[0] == '0':
return '-'
# Set a boolean trigger.
complete = True
# Check for each subject pair.
for required,completed in args[1:]:
if required != completed:
complete = False
return 'Complete' if complete else 'Follow Up'
school_completion('1','6','6','7','7','8','8')
这给了我一个错误ValueError: not enough values to unpack (expected 2, got 1)
,似乎发生在for required,completed in args[1:]
我还尝试让我的函数接受(arg, *args)
(从而避免了切分元组时的任何错误)。那也没用
在序列中解包项要求序列中的每个项都是一个iterable,它生成的项数与接收值的表达式所期望的项数相同。您可以使用
zip
函数在按奇数和偶数索引对序列中的项进行切片后,对它们进行配对更改:
致:
args
是一个元组。只能逐个迭代元组:您只能在ceratin环境下迭代多个项目,例如:
dictionary的
items
方法返回一个特殊的dict_items
对象,可以像这样进行迭代。你不能只处理任何东西,它甚至对元组都没有意义如果您想更具体地说,对象在被迭代时的行为由它在
__iter__
方法中返回的迭代器以及该迭代器在__next__
方法中返回的内容决定。如果它只返回一个值,如元组中的值,则无法将其解压为多个值。在上面的示例中,dict__items
在迭代时返回一个2项元组,因此可以解包相关问题 更多 >
编程相关推荐