Python Forum
Troubles with program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Troubles with program
#31
FYI, I just tried the code below in the Python 2.7 online compiler, and it seems to work:

import random
  
for i in range(100):
    number=random.randrange(1500)
    letters=sorted(random.sample('abcdefgh',4))
    print(str(number) + ' ' + ','.join(letters))
Reply
#32
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.
Reply
#33
@Harambe it is recommended you use Python 3 though - Python 2 isn't supported anymore
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#34
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:

import random

numbers = ... # use sorted(), random.sample(), and range() on this line to create your list of 100 sequential random numbers

for i in range(100):
    letters=sorted(random.sample('abcdefgh',4))
    print(numbers[i]...) # print values from the numbers list sequentially along with your letters list generated within the loop
Try to fill in the blanks where the "..." is shown, and see what you can come up with.
Reply
#35
(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 Big Grin

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.
Reply
#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
#37
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.
Reply
#38
Great job!

(*Edit* - I issued an unnecessary correction after misreading your code. Smile)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling a list troubles giveen 7 3,962 Jan-11-2019, 08:05 PM
Last Post: giveen
  Troubles on how to use Open CV knowledge1st 1 2,501 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