Python输入验证[函数]

2024-05-02 11:47:03 发布

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

我被要求制作一个程序来计算给定大小和方向的结果。。 程序执行计算,但验证中似乎存在问题。。每次我输入一个大于360的值,这是对它要求我再次输入的方向的验证

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

代码:

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 :

Tags: oroftheinputdefmathd2d1