Slartibartfast
Slartibartfast

Reputation: 1190

Counter in for loop and resetting counter

I have the following code:

for i in range(10):
    while True:
       num = int(input("Enter an integer: "))
       print("The double of",num,"is",2 * num)
    print('10')

What i am want to do is after 10 iterations, message should print 10. It does that but only once, how can i reset the counter so it will start again after it reaches 10?

What i want the program to do is print '10' after 10 iterations but the loop is infinite so it never breaks.

Upvotes: 2

Views: 4892

Answers (1)

Marsilinou Zaky
Marsilinou Zaky

Reputation: 1047

You can use this, you would loop only once and check the counter if it's divisable by 10 to print the message

for i in range(1, 100):
    num = int(input("Enter an integer: "))
    print("The double of",num,"is",2 * num)
    if i%10==0:
        print('10')

If you want the infinit loop:

i = 1
while True:
    num = int(input("Enter an integer: "))
    print("The double of",num,"is",2 * num)
    if i%10==0:
        print('10')
    i+=1

The result is for for i in range(1,21) will be

The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
10
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
The double of 50 is 100
10

Upvotes: 1

Related Questions