TypeError: can't multiply sequence by non-int of type 'str'

后端 未结 3 1540
醉话见心
醉话见心 2020-12-11 23:57
>>> 
Enter muzzle velocity (m/2): 60
Enter angle (degrees): 45
Traceback (most recent call last):
  File \"F:/Python31/Lib/idlelib/test\", line 9, in 

        
相关标签:
3条回答
  • 2020-12-12 00:16
    >>> '60' * '60'
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: can't multiply sequence by non-int of type 'str'
    

    You are trying to multiply two strings together. You must convert the string input from the user to a number using int() or float().

    Also, I'm not sure what you're doing with decimal; it looks like you're trying to call the module (the type is in the module, decimal.Decimal) but there's not much point in converting to a Decimal after doing some floating point math and then converting back to a float.

    In the future, post the code that causes the problem (and keep the interaction and traceback). But first try and shrink the code as much as possible while making sure it still causes the error. This is an important step in debugging.

    0 讨论(0)
  • 2020-12-12 00:27

    You are using raw_input() for getting the input. Instead use input(). It will return an Int. Make sure that you input only numbers or input() will raise an error (say in case of a string). Also, it would be nice if you name your variables properly. x and y don't convey much. (velocity and angle would be so much better)

    0 讨论(0)
  • 2020-12-12 00:32

    You should convert the data you get from console to integers:

    x = int(x)
    y = int(y)
    Distance = float(decimal((2*(x*x))((decimal(math.zsin(y)))*(decimal(math.acos(y)))))/2)
    
    0 讨论(0)
提交回复
热议问题