Python module gives string formatting errors

I am trying to execute a value module in python, but I am getting errors as I interpret the module as a string formatting constant, as far as I know. My initial assumption was to type, but then freezes.

val = pow(a,n,p) val = y1*val val = val % p 

Are these two lines of code relevant to this question. Right now, when I run this, I get: TypeError: not all arguments converted during formatting of the line In the second line.

If I complete val with an integer and print it, then ... it takes a very long time to calculate.

I don't know much about python, I think I missed something simple, but what?

+4
source share
2 answers

If yu gets this error, y1 itself is a string. You cannot perform numerical calculations with strings - when you do "int (y1)" it is not casting, it converts the number represented by the characters inside the string o the actual numerical value - and this is the only way you can perform numerical operations on it .

If this is takin thta log, it is probably because you are trying to convert "y1 * val" to int - this is already wrong - if y1 is a string, "y1 * val" gives y1 concatenated to itself "val" times - that would be true great amount. You must have the value i n "y1" as the number before multiplication - as in:

 val = int(y1) * val 
+3
source

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 "<stdin>", line 1, in <module> TypeError: not all arguments converted during string formatting 

I assume y1 is actually a string. Here the difference like y1 does:

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

Source: https://habr.com/ru/post/1402503/


All Articles