Python Forum

Full Version: Unexpected twice output
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys! given that I'm new to Python and coding in general ...

I have the following code, every 5 seconds I want to print Rysource +100, but the output, as you can see, is printed double:

import time

Rysource = 1000

while True:
    for Rysource in range(Rysource, Rysource+101, 100):
        print(Rysource)
    time.sleep(5)
OUTPUT:
1000
1100
1100
1200
1200
1300
1300
1400


Basically it starts with: 1000,1100. After 5 seconds prints: 1100,1200. After 5 seconds prints: 1200,1300
And so on...
How can i print something like this:

OUTPUT:
1000
1100
1200
1300
1400

I hope this is clear and thank you
in each iteration you have 2 numbers in the range object
import time
 
Rysource = 1000

for _ in range(5): # print just first 5 results
    print('You will iterate over: {}'.format(list(range(Rysource, Rysource+101, 100))))
    for Rysource in range(Rysource, Rysource+101, 100):
        print(Rysource)
Output:
You will iterate over: [1000, 1100] 1000 1100 You will iterate over: [1100, 1200] 1100 1200 You will iterate over: [1200, 1300] 1200 1300 You will iterate over: [1300, 1400] 1300 1400 You will iterate over: [1400, 1500] 1400 1500
I'm really unclear on what you are trying to do beyond printing numbers in steps of 100 from a start point ... until you get bored and stop it. What the self referencing for loop is for, I have no idea.

What's wrong with:

from time import sleep
Rysource = 1000
large_number = 9999
 
for counter in range(Rysource, large_number, 100):
    print(counter)
    sleep(5)

If you really want it to go on for ever ... use a while loop, rather than a specific range in a for loop:

from time import sleep
Rysource = 1000
 
while True:
    print(Rysource)
    Rysource += 100
    sleep(5)
Thanks.

Quote:I'm really unclear on what you are trying to do beyond printing numbers in steps of 100 from a start point ... until you get bored and stop it
This was just an experiment, I wanted to print Rysource + 100 every certain amount of time until the end of the programm. Then I wanted to print something else meanwhile Rysource get printed.