How to imitate "until... you do" command with python?

Asked

Viewed 112 times

1

How can I make a counter regressing with python?

I tried so:


count = int(10)  
    while True:  
        print("Eu conto", count)  
        count = int(count -1)
        if count = 0:  
            break

but it seems that it does not accept the -1. How could I make it work?

  • 2

    The comparison operator is the ==, the operator = is the allocation operator. Use: if count == 0: .

  • Ah, it’s true, thank you!

  • 2

    It is also not necessary to inform that the number 10 is of type int because Python already knows. If you want you can remove it.

  • I figured I wouldn’t have to, but I was making a mistake so I put.

  • Why did you use the int() function if the numbers are already integers?

  • Why not put the comparison on while? Would remove the if and the break within it

  • It’s just that I was trying to find a function that did the checking only after the code execution.

Show 2 more comments

1 answer

5


You can use the for for this, I will put an example below.

for count in range(10, -1, -1):
    print("Eu conto", count)

is quite simple

in addition you can add the team, I will give an example below

import time

for count in range(10, -1, -1):
    print("Eu conto", count)
    time.sleep(1)

doing so wait 1 second each time you count a number

and another way down to a line would be like this

print(*[f'Eu conto {count}' for count in range(10, -1, -1)], sep='\n')

then just choose the one that suits you best and make your modifications


Explaining the issue of range for better understanding

range te a generating function that iterates according to past variables, which can only be stop, for example range(10) account from 0 to 9, remembering that it does not go to the end, or we can inform the numbers that will start (start), stop (stop) and when numbers it jumps, for example range(2, 20, 5) he will start counting from number 2 and finish at number 20, but he will jump from 5 to 5 giving you a result equal to [2, 7, 12, 17]

I hope you were able to explain a little.

  • I didn’t know that one. Thank you very much!

  • 1

    there are more options but I believe that this is the simplest to understand, but any doubt just say, I will add one more way, only 1 min

Browser other questions tagged

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