我有一个任务,我要做一个计算合力的程序。问题是它在没有验证的情况下工作,但是当我开始输入一个验证函数时,它一直以“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=,它就会工作。但在这样做时,验证不会超出代码的范围
validate_direction
和validate_magnitude
不返回任何内容。 例如:这是向列表中添加内容,该列表已传入(对它的引用)。 如果我改为
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/)
以此为例:
val
这里实际上没有变化。所以可以用return 123.12312
来代替如果为
return
赋值,而实际上没有值,则会得到以下结果:为了在返回时更改
val
,我们可以使用return
语句,因为它是按值传递的:对于
validate_direction
和validate_magnitude
,您需要返回这些值,原因有两个:return
赋值。因为Python中的默认值是None
,所以D
和M
正在转换为None
李>改为:
相关问题 更多 >
编程相关推荐