Python Modulus Giving String Formatting Errors

后端 未结 2 503
别跟我提以往
别跟我提以往 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:14

    If yu are getting this error, y1 itself is a string. You can't perform numeric calculations with strings - when you do "int(y1) " - it is not casting, it is converting the number represented by characters inside the string o an actual numeric value - and that is the only way you can perform numeric operations on it.

    If it is takin thta log, it is probable because you are trying convert "y1 * val" to int - which is wrong already - if y1 is a string, "y1 * val" gives you y1 concatenated to itself "val" times - so it would be a really huge number. You need to have the value i n "y1" as a number before multiplying - as in:

    val  = int(y1) * val
    

提交回复
热议问题