我有下面的代码,我知道这个函数接受2个整数,所以(n : int, m : int),它返回一个包含任意长度的整数的元组,所以-> tuple[int,...]
我认为这对于空的元组T : tuple[()]也是正确的,因为在函数中的某个点上的元组只有一个值。
然而,我得到了这个错误
main.py:14: error: Incompatible types in assignment (expression has type "Tuple[int]", variable has type "Tuple[]")我遗漏了什么?
def vectorise(n : int, m : int) -> tuple[int,...]:
'''creates a tuple of element n repeated m times'''
T : tuple[()] = () #empty tuple
counter : int = 0
while counter < m:
T = T + (n,) #in Python (n,) is a tuple with one element n
counter+=1
#as a result, T can be of arbitrary length
return T
L = vectorise(100, 5)发布于 2021-11-02 11:41:37
变量T的类型为“空的元组”。您正在尝试为其分配非空元组的内容。因此,您会得到一个类型错误。
您需要确保只将空元组分配给T,或者确保T的类型允许非空元组。
https://stackoverflow.com/questions/69809749
复制相似问题