>>>
Enter muzzle velocity (m/2): 60
Enter angle (degrees): 45
Traceback (most recent call last):
File "F:/Python31/Lib/idlelib/test", line 9, in <module>
range()
File "F:/Python31/Lib/idlelib/test", line 7, in range
Distance = float(decimal((2*(x*x))((decimal(math.zsin(y)))*(decimal(math.acos(y)))))/2)
TypeError: can't multiply sequence by non-int of type 'str'
Run Code Online (Sandbox Code Playgroud)
我只是新手,所以如果真的很明显,不要太苛刻,但为什么我会收到这个错误?
>>> '60' * '60'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'str'
Run Code Online (Sandbox Code Playgroud)
您试图将两个字符串相乘.您必须使用int()或将用户输入的字符串转换为数字float().
另外,我不确定你在做什么decimal; 看起来你正试图调用模块(类型在模块中decimal.Decimal),但在进行一些浮点数学然后转换回a 之后转换为Decimal并没有多大意义.float
将来,发布导致问题的代码(并保持交互和回溯).但首先尝试尽可能缩小代码,同时确保它仍然导致错误.这是调试中的重要一步.
您应该将从控制台获得的数据转换为整数:
x = int(x)
y = int(y)
Distance = float(decimal((2*(x*x))((decimal(math.zsin(y)))*(decimal(math.acos(y)))))/2)
Run Code Online (Sandbox Code Playgroud)