0
Help! Whatâs the difference?
#1 Import random For x in range(5): value=random.randint(1,6) print(value) #2 Import random For x in range(5): x=random.randint(1,6) print(x) What is the difference between the first and second? Why is x used in #1 when it is not referred to anywhere else in the next lines of code? Shouldnât the random integer generated in #2 be in the range of (0,5) how can 6 appear? Thanks for the much needed help!
4 RĂ©ponses
+ 2
There is no difference, that is, the printed result is the same.
for x in range (5): means that it will loop from 0 to 4.
In the first loop x will be 0, 1, 2, 3 and 4, and value gets random values between 1 and 6. In the second one x will also be 1, 2, 3, 4 and 5 but in between it also gets the random values.
If you run the samples like this you will see a difference:
#1
import random
for x in range(5):
print(x)
value=random.randint(1,6)
print(value)
#2
import random
for x in range(5):
print(x)
x=random.randint(1,6)
print(x)
+ 2
It is for the number of loops indeed.
0
Thanks for the reply, so the âfor x in range(5):â is actually redundant? Or is it required to tell how many loops the print should run for?
0
Alright thanks for the help!