Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unexpected twice output
#1
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
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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)
I am trying to help you, really, even if it doesn't always seem that way
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unexpected output Starter 2 439 Nov-22-2023, 12:08 AM
Last Post: Starter
  Unexpected Output - Python Dataframes: Filtering based on Overlapping Dates Xensor 5 654 Nov-15-2023, 06:54 PM
Last Post: deanhystad
  Unexpected output while using random.randint with def terickson2367 1 467 Oct-24-2023, 05:56 AM
Last Post: buran
  Unexpected output from df.loc when indexing by label idratherbecoding 6 1,125 Apr-19-2023, 12:11 AM
Last Post: deanhystad
  unexpected output asyrafcc99 0 1,478 Oct-24-2020, 02:40 PM
Last Post: asyrafcc99
  Unexpected output: symbols for derivative not being displayed saucerdesigner 0 2,011 Jun-22-2020, 10:06 PM
Last Post: saucerdesigner
  Unexpected output palladium 4 2,695 Jan-11-2020, 03:26 PM
Last Post: palladium
  Unexpected output: if statement CabbageMan 1 1,728 Sep-04-2019, 04:12 PM
Last Post: ThomasL
  Unexpected Output using classes and inheritance langley 2 1,912 Jul-04-2019, 09:33 AM
Last Post: langley
  float multiplication - unexpected output inesk 3 3,253 Dec-11-2018, 10:59 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020