函数不断返回,类型错误为“nonetype”

2024-06-01 12:03:47 发布

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

我有一个任务,我要做一个计算合力的程序。问题是它在没有验证的情况下工作,但是当我开始输入一个验证函数时,它一直以“nonetype”的形式返回,所以程序无法编译

代码:

import math
def main():
    M1,D1 = get_values()
    M2,D2 = get_values()
    RFX = rx(M1,M2,D1,D2)
    RFY = ry(M1,M2,D1,D2)
    ResultantMagnitude = resultant(RFX,RFY)
    ResultantDirection = direction_r(RFY,RFX)
    display(ResultantMagnitude,ResultantDirection)
def get_values():
    print('\nPlease input the needed values for the resultant \n ')
    M = float (input('Magnitude of Force = '))
    M = validate_direction(M)
    D = float (input('Direction of Force = '))
    D = validate_direction(D)
    return M,D
def validate_direction(D1):
    while D1 > 360 or D1 < 0:
        print("Invalid Direction, enter again : ")
        D1=float(input())
def validate_magnitude(M1):
    while M1 < 0:
        print("Invalid Magnitude, enter again : ")
        M1=float(input())
def rx(M1,M2,D1,D2):
    #Force 1
    if D1 <= 90 or D1 == 360:
        F1x = ((M1 * math.cos(math.radians(D1))))
    elif D1 <= 180 or D1 > 90:
        F1x = ((abs(M1)* math.cos(math.radians(D1))))
    elif D1 <= 270 or D1 >180:
        F1x = ((M1 * math.cos(math.radians(D1))))
    else:
        F1x = ((M1 * math.cos(math.radians(D1))))
    #force 2
    if D2 <= 90 or D2 == 360:
        F2x = ((M2 * math.cos(math.radians(D2))))
    elif D2 <= 180 or D2 > 90:
        F2x = ((abs(M2)* math.cos(math.radians(D2))))
    elif D2 <= 270 or D2 >180:
        F2x = ((M2 * math.cos(math.radians(D2))))
    else:
        F2x = ((M2 * math.cos(math.radians(D2))))
    RFX = (F1x + F2x)
    return RFX
def ry(M1,M2,D1,D2):
    #Force 1
    if D1 <= 90 or D1 == 360:
        F1y = (M1 * math.sin(math.radians(D1)))
    elif D1 <= 180 or D1 > 90:
        F1y = (abs(M1) * math.sin(math.radians(D1)))
    elif D1 <= 270 or D1 >180:
        F1y = (M1 * math.sin(math.radians(D1)))
    else:
        F1y = (abs(M1) * math.sin(math.radians(D1)))
    #force 2
    if D2 <= 90 or D2 == 360:
        F2y = (M2 * math.sin(math.radians(D2)))
    elif D2 <= 180 or D2 > 90:
        F2y = (abs(M2) * math.sin(math.radians(D2)))
    elif D2 <= 270 or D2 >180:
        F2y = (M2 * math.sin(math.radians(D2)))
    else:
        F2y = (abs(M2) * math.sin(math.radi`enter code here`ans(D2)))
    RFY = (F1y + F2y)
    return RFY
def resultant(RFX,RFY):
    ResultantMagnitude = (math.sqrt((pow(RFX,2) + pow(RFY,2))))
    return ResultantMagnitude
def direction_r(RFY,RFX):
    ResultantDirection =math.degrees(math.atan((RFY)/(RFX)))
    return ResultantDirection
def display(ResultantMagnitude,ResultantDirection):
    print('\n')
    print('The magnitude of the resultant is {:0.2f}'.format(ResultantMagnitude), 'Newton')
    print('The direction of the resultant is {:0.2f}'.format(ResultantDirection) , 'Degrees')

if __name__ == '__main__':
    main()

错误:

  Please input the needed values for the resultant 

    Magnitude of Force = 200
    Direction of Force = 200

    Please input the needed values for the resultant 

    Magnitude of Force = 200
    Direction of Force = 200
    Traceback (most recent call last):
      File "C:/IntelliJ/Python/Activity1/Test Force.py", line 78, in <module>
        main()
      File "C:/IntelliJ/Python/Activity1/Test Force.py", line 5, in main
        RFX = rx(M1,M2,D1,D2)
      File "C:/IntelliJ/Python/Activity1/Test Force.py", line 27, in rx
        if D1 <= 90 or D1 == 360:
    **TypeError: '<=' not supported between instances of 'NoneType' and 'int'**

Process finished with exit code 1

“D1”、“D2”、“M1”、“M2”一直以非类型返回 当我移除D1=,D2=,M1=,M2=,它就会工作。但在这样做时,验证不会超出代码的范围


Tags: orofthedefmathcosd2d1
1条回答
网友
1楼 · 发布于 2024-06-01 12:03:47

validate_directionvalidate_magnitude不返回任何内容。 例如:

def add_to_my_list(my_list, val):
    my_list.append(val)

l = []
add_to_my_list(l, 1) # l now is [1]

这是向列表中添加内容,该列表已传入(对它的引用)。 如果我改为l = add_to_my_list(l, 1),那么就没有显式的return语句。在这种情况下,您需要设置l = None,因为Python中的默认返回值是None

如果要修改函数中的值,需要确保该值是将通过引用传递的对象(通过对象引用传递:https://robertheaton.com/2014/02/09/pythons-pass-by-object-reference-as-explained-by-philip-k-dick/

以此为例:

In [375]: def modify(val): 
 ...:     val = 123.12312 
 ...:      
 ...:                                                                                                                 

In [376]: val = 5.1                                                                                                       

In [377]: modify(val)                                                                                                     

In [378]: val                                                                                                             
Out[378]: 5.1

val这里实际上没有变化。所以可以用return 123.12312来代替

如果为return赋值,而实际上没有值,则会得到以下结果:

In [379]: def modify(val): 
 ...:     val = 123 
 ...:                                                                                                                 

In [380]: val = 5                                                                                                         

In [381]: val = modify(val)                                                                                               

In [382]: val

In [383]: val is None                                                                                                     
Out[383]: True

为了在返回时更改val,我们可以使用return语句,因为它是按值传递的:

In [384]: def modify(val): 
 ...:     return 123 
 ...:                                                                                                                 

In [385]: val = 5                                                                                                         

In [386]: val = modify(None)                                                                                              

In [387]: val                                                                                                             
Out[387]: 123

对于validate_directionvalidate_magnitude,您需要返回这些值,原因有两个:

  1. 传入的值正在按值传递
  2. 您正在为这些函数调用的return赋值。因为Python中的默认值是None,所以DM正在转换为None

改为:

def validate_direction(D1):
    while D1 > 360 or D1 < 0:
        print("Invalid Direction, enter again : ")
        D1=float(input())
    return D1
def validate_magnitude(M1):
    while M1 < 0:
        print("Invalid Magnitude, enter again : ")
        M1=float(input())
    return M1

相关问题 更多 >