May-28-2020, 01:09 PM
You can use sorted() to get your list of letters in order, and you can use join() to handle formatting the output correctly. Also, you really don't need your variable assignments in lines 2 and 3 since you handle those in your loop. Here is my doctored-up version of your code:
import random for i in range(100): number=random.randrange(1500) letters=sorted(random.sample('abcdefgh',4)) print(number, ','.join(letters))Note that for number, since you only need a single value, you can reduce the complexity of the assignment by just using random.randrange(). The random.sample() function can sample from a string as well as a list, so 'abcdefgh' works just as well as a list of the individual letters. The sorted() function ensures that your letters are in the correct order. And finally, the join() function is called as sep.join() where sep is the string you want to use in between your joined values. So in this case, a comma and a space.