Add a number to another

Asked

Viewed 39 times

-4

I was wondering if anyone knows how to add a number to another in Python, for example in the code I’m doing:

Write a python script that does the following:

1 * 8 + 1 = 9

12 * 8 + 2 = 98

123 * 8 + 3 =987

the values of the first multiplication term and the result shall be calculated by the program

  • Hello Fox! Welcome to Stackoverflow in English! Your question is unclear. If you can improve your explanation, we can help.

  • Hi Daniel, I’m sorry and I’ve already edited.

  • Nothing is clear yet. What is the rule for defining the "first term of multiplication"?

  • I have no idea it was in the statement I received, but I already solved the problem thanks in the same soma=1 for i in range(1,10): print(sum,"x 8+",i,"=",sum*8+i) sum = sum * 10 + i + 1

1 answer

0


Okay, if I understand correctly, your wish would be to "concatenate" the meters of the is to multiply by eight. In case this is possible yes, just convert the value of the counter to a string and concatenate with the previous counter. For this you will need a variable that at each loop will do the concatenation. Here is an example:

multiplicador = ''
for i in range(1, 4):
    # aqui você deve concatenar a string multiplicador com o contador i
    # como i é do tipo inteiro, você deve converte-lo para string com a função str()
    multiplicador += str(i)
    
    # agora para fazer o calculo é necessário converter a variável multiplicador
    # para um tipo inteiro com a função int()
    print(f'{multiplicador} * 8 + {i} = {int(multiplicador) * 8 + i}')

So the result will be

1 * 8 + 1 = 9

12 * 8 + 2 = 98

123 * 8 + 3 = 987
  • Thank you very much!!!!

  • I recently made a correction to the code. Previously in the print() function I put the variable name "counter" instead of "i" as I had stated before.

  • I noticed, but thanks anyway, I managed to get there too by another easier solution: soma=1 for i in range(1,10): print(sum,"x 8+",i,"=",sum*8+i) sum = sum * 10 + i + 1

  • 1

    Do not do the users' homework. This is not the purpose of the site and also does not help the author of the question. Do not answer questions whose users post exercise statements that do not publish a [MCVE] punctuating a difficulty. Ref: Thinking we’ll do all your work for free. Behold What is the Stack Overflow? and Stack Overflow Survival Guide in English

Browser other questions tagged

You are not signed in. Login or sign up in order to post.