Python Forum
Troubles with program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Troubles with program
#36
(May-28-2020, 04:37 PM)Harambe Wrote:
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:
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:
print(str(number[i]) + '. ' + ','.join(letters))
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,054 Jan-11-2019, 08:05 PM
Last Post: giveen
  Troubles on how to use Open CV knowledge1st 1 2,571 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