I ran the following code on Python 2 and Python 3
i = int(input())
for j in range(i):
k = int(input())
print(k*k)%(1000000000+7)
the input was was fed from a file containing the following
2
2
1
The Python 2 version ran fine but Python 3 gave this error
4
Traceback (most recent call last):
File "solution.py", line 4, in <module>
print(k*k)%(1000000000+7)
TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'
why is this error occurring in Python 3 and how do i correct it
printis a function, sok*kis an argument toprint()and then the result of printing (None) is given the%operator. To fix, just put parentheses around the whole thing (excluding the actualprintword, of course).(k*k)% (1000000000+7)you have to wrap the whole thing in parenthesis:print((k*k) % (1000000000+7)). BTW: this produces the same result in both python and python3. (The output changes when you start using commas or keyword arguments...)