Python Forum
Unexpected twice output - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Unexpected twice output (/thread-13139.html)



Unexpected twice output - Samo - Sep-29-2018

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


RE: Unexpected twice output - buran - Sep-29-2018

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



RE: Unexpected twice output - gruntfutuk - Sep-29-2018

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)



RE: Unexpected twice output - Samo - Sep-29-2018

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.