Posts: 150
Threads: 3
Joined: Apr 2020
FYI, I just tried the code below in the Python 2.7 online compiler, and it seems to work:
1 2 3 4 5 6 |
import random
for i in range ( 100 ):
number = random.randrange( 1500 )
letters = sorted (random.sample( 'abcdefgh' , 4 ))
print ( str (number) + ' ' + ',' .join(letters))
|
Posts: 16
Threads: 2
Joined: May 2020
May-28-2020, 01:58 PM
(This post was last modified: May-28-2020, 01:58 PM by Harambe.)
I would never do it without your help lol. Mainly the 6th line. But I still dont understand how did we prevent numbers to generate unique... And how to sort them like the letters. I tried to use “sorted” command for numbers too but no action.
Posts: 353
Threads: 13
Joined: Mar 2020
May-28-2020, 02:26 PM
(This post was last modified: May-28-2020, 02:26 PM by pyzyx3qwerty.)
@ Harambe it is recommended you use Python 3 though - Python 2 isn't supported anymore
Posts: 150
Threads: 3
Joined: Apr 2020
Oops, I didn't realize the numbers needed to be unique! The code as it currently stands could result in the same number being selected more than once. To ensure there are no duplicates, it might be best to first create a list of random numbers outside of your loop, and then just print each one sequentially within your loop. I think you probably know everything you need to do this based on your previous code. You'd want to do something like:
1 2 3 4 5 6 7 |
import random
numbers = ...
for i in range ( 100 ):
letters = sorted (random.sample( 'abcdefgh' , 4 ))
print (numbers[i]...)
|
Try to fill in the blanks where the "..." is shown, and see what you can come up with.
Posts: 16
Threads: 2
Joined: May 2020
May-28-2020, 04:37 PM
(This post was last modified: May-28-2020, 04:37 PM by Harambe.)
(May-28-2020, 02:26 PM)pyzyx3qwerty Wrote: @Harambe it is recommended you use Python 3 though - Python 2 isn't supported anymore
I wanted to but my iPad has problems with the new version
1 2 3 4 5 |
import random
number = sorted (random.sample( range ( 1 , 1501 )))
for i in range ( 100 ):
letters = sorted (random.sample( 'abcdefgh' , 4 ))
print ( str (number) + '.' + ' ' + ',' .join(letters))
|
Error in 2nd line. I dont know why. Range command includes start, stop and count value. Didnt write 1 as count cause it is given automatically.
Posts: 150
Threads: 3
Joined: Apr 2020
(May-28-2020, 04:37 PM)Harambe Wrote:
1 2 3 4 5 |
import random
number = sorted (random.sample( range ( 1 , 1501 )))
for i in range ( 100 ):
letters = sorted (random.sample( 'abcdefgh' , 4 ))
print ( str (number) + '.' + ' ' + ',' .join(letters))
|
Error in 2nd line. I dont know why. Range command includes start, stop and count value. Didnt write 1 as count cause it is given automatically.
It is the random.sample() function that needs an additional argument, not range(). Remember that random.sample() is going to return a list, and the second argument when calling it is the number of items you want included in that list. In this case, you want a list of 100 random numbers so:
1 |
number = sorted (random.sample( range ( 1 , 1501 ), 100 ))
|
In your print statement, you want to print individual items from the list variable "number" (using the index value) rather than the whole list at once, so use "number[i]" here. Also, you can combine the period and space into one string rather than including each of them separately:
1 |
print ( str (number[i]) + '. ' + ',' .join(letters))
|
Posts: 16
Threads: 2
Joined: May 2020
May-28-2020, 05:05 PM
(This post was last modified: May-28-2020, 05:06 PM by Harambe.)
1 2 3 4 5 |
import random
number = sorted (random.sample( range ( 1 , 1501 ), 100 ))
for i in number:
letters = sorted (random.sample( 'abcdefgh' , 4 ))
print ( str (i) + '.' + ' ' + ',' .join(letters))
|
Finally!
Edit: Haha, did it few minutes ago :) Thank you so much.
Posts: 150
Threads: 3
Joined: Apr 2020
May-28-2020, 05:12 PM
(This post was last modified: May-28-2020, 05:12 PM by GOTO10.)
Great job!
(*Edit* - I issued an unnecessary correction after misreading your code.  )
|