Python输入验证[功能]

时间:2020-04-13 04:04:10

标签: python function validation

我被要求编写一个程序,计算给定幅度和方向的结果。 该程序执行了计算,但是验证似乎存在问题。每当我输入的输入大于360时(这是对该方向的验证,它将要求我再次输入

问题: 即使我输入的幅度不能超过360,它也会继续询问方向。与我对幅度不能小于0的验证相同,如果我输入的数字小于0,即使它应该是幅度,它也会要求DIRECTION输入了错误的信息

代码:

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 ')
    D = float (input('Direction of Force = '))
    D = validate_direction(D)
    M = float (input('Magnitude of Force = '))
    M = validate_direction(M)
    return M,D
def validate_direction(Dz):
    while Dz > 360 or Dz < 0:
        print("Invalid Direction, enter again : ")
        Dz=float(input())
    return Dz
def validate_magnitude(Mz):
    while Mz < 0:
        print("Invalid Magnitude, enter again : ")
        Mz=float(input())
    return Mz
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.radians(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):
    if RFY == 0:
        RFY = 1
        if RFX == 0:
            RFX = 1

    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')


x="Y"
while(x!="N"):
    main()
    x=input("Press Y to continue, N to stop : ").upper()

输出:

Please input the needed values for the resultant

Direction of Force = 200
Magnitude of Force = 2000
Invalid Direction, enter again :
200

Please input the needed values for the resultant

Direction of Force = 200
Magnitude of Force = -200
Invalid Direction, enter again :
200


The magnitude of the resultant is 400.00 Newton
The direction of the resultant is 20.00 Degrees
Press Y to continue, N to stop :

1 个答案:

答案 0 :(得分:1)

您要调用两次validate_direction()函数。您应该为M调用validate_magnitude()。

相关问题