Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3函数参数验证_Python - Fatal编程技术网

Python3函数参数验证

Python3函数参数验证,python,Python,怎么了?第一个参数-num_of_edges我需要int类型,其他参数为float import math def polygon(num_of_edges, radius, startx, starty, turn_angle): try: num_of_edges = int(num_of_edges) radius = float(radius) startx = float(startx)

怎么了?第一个参数-num_of_edges我需要int类型,其他参数为float

import math
def polygon(num_of_edges, radius, startx, starty, turn_angle):
        try:
            num_of_edges = int(num_of_edges)
            radius = float(radius)
            startx = float(startx)
            starty = float(starty)
            turn_angle = float(turn_angle)
        except ValueError:
            print("All parameters not corect!")
        else:
            turn_angle = 90 - turn_angle 
            pirmais_angle = math.radians(turn_angle)
            points = []
            for i in range(num_of_edges):
                angle = 2*math.pi*(-i)/num_of_edges+pirmais_angle 
                x = math.cos(angle)*radius+startx
                y = math.sin(angle)*radius+starty
                points.append((x, y))
            return points

当我使用建议的输入运行时,我得到

polygon(a,3,e,0,0)

NameError: name 'a' is not defined
这里的问题是,它甚至没有进入你的功能。Python已经知道
a
是未定义的,它甚至拒绝调用polygon。所以多边形中的测试甚至没有尝试过

如果它能够进入函数,您仍然会得到
名称错误
,而不是
值错误
。因此,您的
除外
测试错误


注意:不能保证为所有错误输入获取特定的错误类型。对于exmaple
float(1+1j)
(复数的float)会引发
TypeError

我们应该如何知道什么是错误的?您还没有告诉我们尝试时会发生什么,或者会出现什么错误。多边形(a,3,e,0,0)如果这样调用函数,第一个和第三个参数是不正确的,但是打印(“所有参数都不正确!”)该行不起作用。我使用一些输入运行代码,并给出输出。如果有问题,请说明您使用的输入是什么,您得到的输出是什么,您期望得到的输出是什么。错误的回溯是什么?有什么错误吗?