0
Import random
#range(6), random.randint(1, 49), : whats else it needs to stop printing a repeat nums? in range of 6
16 ответов
+ 2
Mohamed, why the trailing `else` clause at the end of the `while` block? It's doing essentially nothing.
Anyway, kenji, what about this?
from random import randint
seen = set()
while len(seen) < 6:
seen.add(randint(1, 49))
print(seen)
+ 2
from random import randint
a = []
for i in range(6):
x = randint(1, 49)
if x not in a:
a. append(x)
print(a)
+ 2
from random import randint
a = []
counter = 0
while counter < 6:
x = randint(1, 49)
if x not in a:
a. append(x)
counter +=1
else:
pass
print(a)
+ 2
thx a lot DZ, now I know
+ 2
you are welcome :)
+ 2
I knew that, I just wanted to do that for him to see if he will notice that or not hhhh that's all.
+ 1
Use a set comprehension, sets can't have repeated values, i.e.,
from random import randint
uniques = {randint(1, 49) for _ in range(6)}
+ 1
DZ seems The best ans, but the output is sometimes 4 nums or 5 nums only in range(6), can't get it?
+ 1
Ah lol I see, I ruined it.
0
import random
for i in range (6):
print ( random.randint(1, 49))
#output, 3 , 15 , 30, 3, 16, 47
#how can it not output a num two times, e.g '3'?
0
import random
arr=[]
f=0
while True :
a=random.randint(1,49)
if a not in arr :
print(a)
arr.append(a)
f+=1
if f==6 :
break
0
@ kenji
i posted an answer before...
its seems perfect ....
it always prints 6 different numbers
0
hah got me! I just a newbie, wondering the random function should have some kind of methods to do all the work not repeated the nums, would it be without a loops.
0
Edged yours more clean, thx a lot!
0
#how about this
import random
num = list (range (1, 50))
picks = random.sample (num, k=6)
print (picks)
- 1
exactly what u want
be more specific please