Python Modulus Giving String Formatting Errors

后端 未结 2 478
别跟我提以往
别跟我提以往 2021-01-18 13:02

I\'m trying to perform the modulus of a value in python, but I\'m getting errors as it\'s interpretting the modulus as a string formatting constant, from my knowledge. My in

2条回答
  •  不思量自难忘°
    2021-01-18 13:22

    As you can see from this code, the % operator has different meanings with strings than with numbers.

    >>> 1 % 2
    1
    >>> '1' % 2
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: not all arguments converted during string formatting
    

    My guess is that y1 is actually a string. Here's the difference the type of y1 makes:

    >>> val = 10
    >>> y1 = '2'
    >>> val * y1
    '2222222222'
    >>> y1 = 2
    >>> val * y1
    20
    

提交回复
热议问题