How to operate on the counter of a while in each loop iteration

Asked

Viewed 126 times

-3

Sorry my ignorance I’m starting to learn python now, my doubt is: How can I generate a result in each iteration of a while.

valor= input('digite o numero desejado:')

contador = 0

while contador <=100:

print(contador, '% de', valor,' é igual a:', **'RESULTADO'**)
contador= contador + 1 

I want to find a function or command that print the term RESULT for all values from 1 to 100.

  • Helped a lot, I managed to finish my project and still learned something new, I don’t know how to thank.

2 answers

3


You can use comprehensilist on to obtain the value and f-strig to simplify the composition of the text.

valor= int(input('digite o numero desejado:'))

#No caso 0% e 100% são valores redundante pois 0% sempre vai ser 0 e 100% é o valor da entrada
for p, r in [(i, valor * i / 100) for i in range(1,100)]:
  print(f'{p}% de {valor} é igual a {r}')

or else:

valor= int(input('digite o numero desejado:'))

#No caso 0% e 100% são valores redundante pois 0% sempre vai ser 0 e 100% é o valor da entrada
for r in [(i, valor * i / 100) for i in range(1,100)]:
  print(f'{r[0]}% de {valor} é igual a {r[1]}')

Test both examples in Repl.it: https://repl.it/repls/ExcellentAdvancedSlope

List comprehension is a way to create lists where elements are obtained or operations applied to members of another iterable sequence or satisfying a certain condition.

Strings-f are literal strings prefixed by the letter 'f' or 'F' where expressions appear between keys {and } is an expression of the language to be evaluated.

  • Those who voted negative would like to explain what is wrong with the answer so that it can be improved.

  • Thanks for the tips I will study on these subjects that have been addressed.

  • 1

    @Viniciussartori anything is just asking a new question that the community is always willing to help.

  • Augusto you could explain how I could now " search " a certain value among these values, let’s say that at the end of the code I create an input that I would like to find a specific number as for example. ' show the value of 31% only ' but the previous result list is not gone. ps: I only changed the range to run the code because I wanted the 100% value to be shown at the end, even though I knew it is the same value as the input number.

  • @Viniciussartori Seria print(valor * número_desejado / 100) in case 31% would be print(valor * 31 /100).

1

A simple mathematical calculation already solves the problem to find the result. Just divide the value by 100 and then multiply by contador that you get the result.

resultado = valor / 100 * porcentagem

You can improve your code by replacing the loop while by a for...range. This way, you reduce the amount of lines that would be spent creating the counter.

valor = int(input('Digite o numero desejado: '))

for contador in range(0, 101):
    print(str(contador) + '% de', valor, 'é igual a:', valor / 100 * contador)

And the last problem is that the value you get from the user is a string and not an integer number. Python does not perform calculations with string values, so you will need to convert the obtained value into a int() as in the code above.

  • Thank you very much, it was simpler than I expected, without words for the answer so fast, I can already go to sleep calmly without that doubt. Vlw same.

  • Those who voted negative would like to explain what is wrong with the answer so that it can be improved.

Browser other questions tagged

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