-1
n = int(input(""))
for i in range(n):
  a,b = input("").split()
  if a%b!=0:
    r = a%b
    x = b-r
    print(x)
  else:
    print("0")

The error is:

TypeError: not all arguments converted during string formatting

How can I make a and b as integers? Can anyone help me out

ppwater
  • 2,315
  • 4
  • 15
  • 29
Vaibhav Yalla
  • 35
  • 1
  • 4
  • `a` and `b` are strings... `a % b` is a string format operator trying to format `b` into `a`. If you meant for `a` and `b` to be numbers, then the answer is in the link above – Tomerikoo Nov 04 '20 at 11:31

1 Answers1

1

Reason

The error is because if you do input.split() then the result will be this ['3', '5'](let's say the input was 3 5) and a, b are both strings.

If you do % with a string, you will get '3' % '5' and it's not what you expected. This means string formatting in Python and you get the error.

you will get the same error with this:

a = '3'
b = '5'
print(a % b) # this cause the error. you will not get 3 % 5

Solution

You could do int() every time where a and b appears, but It will be too hard. Instead, try this:

n = int(input())
for i in range(n):
    a, b = map(int, input().split())
    if a % b != 0:
        r = a % b
        x = b - r
        print(x)
    else:
        print("0")

You can use the built-in map() function in Python and by the way, if you don't have any prompt for the input, then just leave it blank - input().

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
ppwater
  • 2,315
  • 4
  • 15
  • 29