TypeError:不能将序列乘以'str'类型的非int

2024-05-14 05:41:33 发布

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

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

我只是新来的,所以如果这很明显的话,不要太苛刻,但是为什么我会犯这个错误呢?


Tags: intestliblinerangemathfiledecimal
3条回答
>>> '60' * '60'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'str'

你想把两个字符串相乘。必须使用int()float()将用户输入的字符串转换为数字。

另外,我也不知道你在用decimal做什么;看起来你在试图调用模块(类型是中的模块,decimal.Decimal),但是在做浮点运算之后转换成十进制的没有多大意义,然后再转换回一个float

在将来,发布导致问题的代码(并保持交互和回溯)。但首先要尽量缩小代码,同时确保它仍然会导致错误。这是调试的一个重要步骤。

您应该将从控制台获取的数据转换为整数:

x = int(x)
y = int(y)
Distance = float(decimal((2*(x*x))((decimal(math.zsin(y)))*(decimal(math.acos(y)))))/2)

您正在使用raw_input()获取输入。 而是使用input()。它将返回一个Int。请确保只输入数字,否则input()将引发错误(例如字符串)。 另外,如果你能正确地命名你的变量,那就太好了。x和y表达的不多。(速度和角度会更好)

相关问题 更多 >