函数中的两个向量

2024-09-24 00:28:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python是个新手。你知道吗

我想创建一个包含两个向量的函数。我试过了

def twovectors((velocity1,length1),(velocity2,length2)):

但我有个信息错误

SyntaxError: invalid syntax.

求你了,我需要帮助。你知道吗


Tags: 函数信息def错误向量syntax新手invalid
2条回答

不能将tuple作为参数放入函数定义中。检查Python语言参考中的Multiple Function Arguments8.6. Function definitions。你知道吗

尝试以下操作:

def twovectors(vector1, vector2):
    velocity1, length1 = vector1
    velocity2, length2 = vector2
    # Other code...

我使用tuple unpacking来扩展提供的元组参数。你知道吗

用python编写函数的方式如下:

def twovectors(velocity1, velocity2):
    # You can get the length of those vectors after you get inside the function
    len1, len2 = len(velocity1), len(velocity2)
    // Your code here

    return whateveryouwantto

相关问题 更多 >