Python Forum
Troubles with program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Troubles with program
#22
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.
Reply


Messages In This Thread
Troubles with program - by Harambe - May-27-2020, 10:38 PM
RE: Troubles with program - by Larz60+ - May-27-2020, 11:07 PM
RE: Troubles with program - by Harambe - May-27-2020, 11:09 PM
RE: Troubles with program - by Larz60+ - May-27-2020, 11:17 PM
RE: Troubles with program - by Harambe - May-27-2020, 11:25 PM
RE: Troubles with program - by deanhystad - May-28-2020, 05:12 AM
RE: Troubles with program - by perfringo - May-28-2020, 06:20 AM
RE: Troubles with program - by Knight18 - May-28-2020, 06:34 AM
RE: Troubles with program - by Harambe - May-28-2020, 09:25 AM
RE: Troubles with program - by buran - May-28-2020, 09:34 AM
RE: Troubles with program - by Harambe - May-28-2020, 10:04 AM
RE: Troubles with program - by pyzyx3qwerty - May-28-2020, 10:16 AM
RE: Troubles with program - by Harambe - May-28-2020, 10:25 AM
RE: Troubles with program - by pyzyx3qwerty - May-28-2020, 10:59 AM
RE: Troubles with program - by Harambe - May-28-2020, 11:04 AM
RE: Troubles with program - by pyzyx3qwerty - May-28-2020, 11:13 AM
RE: Troubles with program - by Harambe - May-28-2020, 11:49 AM
RE: Troubles with program - by GOTO10 - May-28-2020, 12:14 PM
RE: Troubles with program - by pyzyx3qwerty - May-28-2020, 12:23 PM
RE: Troubles with program - by Harambe - May-28-2020, 12:52 PM
RE: Troubles with program - by pyzyx3qwerty - May-28-2020, 01:09 PM
RE: Troubles with program - by GOTO10 - May-28-2020, 01:09 PM
RE: Troubles with program - by BitPythoner - May-28-2020, 01:12 PM
RE: Troubles with program - by Harambe - May-28-2020, 01:27 PM
RE: Troubles with program - by GOTO10 - May-28-2020, 01:29 PM
RE: Troubles with program - by Harambe - May-28-2020, 01:32 PM
RE: Troubles with program - by GOTO10 - May-28-2020, 01:36 PM
RE: Troubles with program - by Harambe - May-28-2020, 01:40 PM
RE: Troubles with program - by GOTO10 - May-28-2020, 01:41 PM
RE: Troubles with program - by Harambe - May-28-2020, 01:44 PM
RE: Troubles with program - by GOTO10 - May-28-2020, 01:45 PM
RE: Troubles with program - by Harambe - May-28-2020, 01:58 PM
RE: Troubles with program - by pyzyx3qwerty - May-28-2020, 02:26 PM
RE: Troubles with program - by Harambe - May-28-2020, 04:37 PM
RE: Troubles with program - by GOTO10 - May-28-2020, 05:05 PM
RE: Troubles with program - by GOTO10 - May-28-2020, 02:29 PM
RE: Troubles with program - by Harambe - May-28-2020, 05:05 PM
RE: Troubles with program - by GOTO10 - May-28-2020, 05:12 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling a list troubles giveen 7 4,084 Jan-11-2019, 08:05 PM
Last Post: giveen
  Troubles on how to use Open CV knowledge1st 1 2,586 May-23-2018, 05:57 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020