Youngin Kim
Youngin Kim

Reputation: 15

How can I convert int to str in this code?

t = int (input())
for i in range(t): 
    A,B = map(int,input().split())
    print(int('Case #%d:' + (A+B) %t))

This code gets number of times(t) the loop is going to repeat, gets two numbers from the user and prints out the sum of two numbers

however,

I get a type error that says "must be str not int" how can i fix this?

Upvotes: 1

Views: 51

Answers (4)

Youngin Kim
Youngin Kim

Reputation: 15

cases = int(input())

for i in range(cases):
    a,b = map(int, input().split())
    ans = a + b
    print("Case #%s: %s"%(i+1, ans ))

Thanks for the contribution! so I tried this way and it worked as well!

Upvotes: 0

yasser
yasser

Reputation: 91

try this code

t = int (input())
for i in range(t): 
    A,B = map(int,input().split())
    print('Case #{case}:{sum}'.format(case = i,sum= A+B))

Upvotes: 1

Gustav Rasmussen
Gustav Rasmussen

Reputation: 3961

You cannot make the print statement content into an int, but you can do this instead:

t = int(input())


for i in range(t):
    res = input("input string")
    res = res.split()
    print(res)
    A, B = map(int, res)
    print('Case #%d:' %t + str(A+B))

Returning for t = 2, and res = 2, 3 and 4, 5:

input string2 3
['2', '3']
Case #2:5
input string4 5
['4', '5']
Case #2:9

Upvotes: 2

Alexander Kosik
Alexander Kosik

Reputation: 669

Your print statement is wrong, use this:

t = int (input())
for i in range(t): 
    A,B = map(int,input().split())
    print(f'Case #{i}: {A+B}')

Upvotes: 2

Related Questions