Python Forum
Troubles with program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Troubles with program
#21
random.sample form lists
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
#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
#23
Hi there,

If you would like to generate a random number, I suggest you use random.randrange() and define each
variable as the generated output. Then string them together, or put them in a list.
Reply
#24
Output:
(714, 'b,c,d,e') (616, 'e,f,g,h') (1006, 'a,c,d,g') (71, 'a,e,f,h') (493, 'a,d,f,g') (1184, 'b,c,e,g') ...
Still the same problem with ‘ and (). And one more thing, I dont understand why it write coma after the number. I cant see defined that one.
Reply
#25
Can you post your current code?
Reply
#26
Its actually your code. I tried to modify it but no change. When I changed coma in between apostrophes it just changed the thing between letters. Nothing happened with numbers.
import random
 
for i in range(100):
    number=random.randrange(1500)
    letters=sorted(random.sample('abcdefgh',4))
    print(number,','.join(letters))
Reply
#27
Ah, I see. What version of Python are you using? I'm on 3.8 and my output looks like this:

Output:
420 a,b,c,d 772 c,e,f,g 1159 a,e,f,h 1237 b,e,f,g 948 a,e,f,h 848 d,e,f,h 441 d,e,g,h 336 c,e,f,g 325 c,e,g,h etc....
Reply
#28
Python2IDE on iPad. I used 3, but it started to crash a lot.

Btw. I found the command
reverse=True
to sort the numbers in ascending order but I am not sure how to implement it.
Reply
#29
I haven't used Python 2 (and I believe it is now deprecated), but maybe someone else can help us figure that out.

If you want to try the code in Python 3, you can use the online compiler at https://repl.it/languages/python3.
Reply
#30
(May-28-2020, 01:41 PM)GOTO10 Wrote: I haven't used Python 2 (and I believe it is now deprecated), but maybe someone else can help us figure that out.

If you want to try the code in Python 3, you can use the online compiler at https://repl.it/languages/python3.
Thanks for the link.
Reply


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