我被要求制作一个程序,计算给定幅度和方向的结果。。程序执行计算,但验证中似乎有问题。。每次我输入超过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 :
您调用了两次validate_direction((函数。您应该为M.调用validate_magnitude((