1
votes

Comment puis-je convertir int en str dans ce 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 numbershowever,I get a type error that says "must be str not int"
how can i fix this?

2 commentaires

Je ne sais pas ce que ce print (int ('Case #% d:' + (A + B)% t)) est censé faire


Veuillez mettre à jour votre question avec le suivi complet des erreurs.


4 Réponses :


2
votes

Votre déclaration d'impression est erronée, utilisez ceci:

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


0 commentaires

2
votes

Vous ne pouvez pas transformer le contenu de l'instruction d'impression en un entier, mais vous pouvez le faire à la place:

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

Retour pour t = 2, et res = 2, 3 et 4, 5:

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))


0 commentaires

1
votes

essayez ce 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))


0 commentaires

0
votes
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!

0 commentaires