-2

I want to print the numbers 1 to 10 with a for loop, but import random is affecting my for loop and the loop is running twice. Here is a screenshot of my editor. The output is displayed below. Can you explain to me why this happens?

import random
for i in range(1,10):
    print(i)

My output:

1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
D Malan
  • 10,272
  • 3
  • 25
  • 50

2 Answers2

1

This will never happen.

make sure you are running the latest version of your program, and make sure you didn't paste the same code at some other place in the same file. e.g. make sure you don't have a precompiled version of it. (e.g. change the file name and try again).

Update:

based on the comment by matthias, if you saved your file as random.py the above result could be reproduced.

# if you save the code below as random.py
import random
for i in range(1,10):
    print(i)



# your output could be reproduced as below.
>>> python random.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
 1
 2
 3
 4
 5
 6
 7
 8
 9

solution: change the file name or if you have to import it make sure you include the codition if __name__ == "__main__" in before the for loop of your random.py

Yonas Kassa
  • 3,362
  • 1
  • 18
  • 27
-2

11I do not see any problem, I did the example and it works perfect.

My output: 1 2 3 4 5 6 7 8 9

Could it be that you have called the file twice? It's the only request I find.