+ 3

How to merge tow codes???

Hallo, how to merge the following tow codes without repeating the "for" method AND with same resault: import random for i in range(5): x = random.randint(1, 6) print(x) for i in range(5): y = random.randint(64, 6464) print(y)

3rd May 2020, 2:04 PM
Miko Bacht
Miko Bacht - avatar
8 Answers
+ 5
Here is a comprehension, that should do the job: from random import choices [print(i) for i in choices(range(1,7),k=5)] print() [print(i) for i in choices(range(64,6465),k=5)] # output: 4 6 5 1 2 4545 2674 4494 1708 6232
3rd May 2020, 8:24 PM
Lothar
Lothar - avatar
+ 4
RAJESH SAHU ohh! that works really well.
3rd May 2020, 2:21 PM
M Tamim
M Tamim - avatar
+ 3
Not as perfect as you want import random x,y=[],[] for i in range(5): x.append(random.randint(1,6)) y.append(random.randint(64,6464)) print(*x) print(*y)
3rd May 2020, 2:13 PM
ANJALI SAHU
+ 3
First print like so: 5 3 6 4 2 Second print like so: 979 866 1314 3335 6600 And not like this: 1 3434 5 6622 4 1010 6 793 3
3rd May 2020, 2:13 PM
Miko Bacht
Miko Bacht - avatar
3rd May 2020, 3:00 PM
Miko Bacht
Miko Bacht - avatar
+ 2
Before we can give you an answer, please tell us what the code should do. Give us a sample for input values and a sample how the output should looks like. Thanks!
3rd May 2020, 2:11 PM
Lothar
Lothar - avatar
0
Concatenating the number-ranges would also be an option: from random import randint for r in [(1,6)] * 5 + [(64, 6464)] * 5: print(randint(*r)) https://code.sololearn.com/cKu5UvQCoeEq/?ref=app
5th May 2020, 11:00 AM
Manu_1-9-8-5
Manu_1-9-8-5 - avatar