Posts: 354
Threads: 13
Joined: Mar 2020
Posts: 151
Threads: 3
Joined: Apr 2020
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.
Posts: 36
Threads: 0
Joined: May 2020
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.
Posts: 16
Threads: 2
Joined: May 2020
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.
Posts: 151
Threads: 3
Joined: Apr 2020
Can you post your current code?
Posts: 16
Threads: 2
Joined: May 2020
May-28-2020, 01:32 PM
(This post was last modified: May-28-2020, 01:32 PM by Harambe.)
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))
Posts: 151
Threads: 3
Joined: Apr 2020
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....
Posts: 16
Threads: 2
Joined: May 2020
May-28-2020, 01:40 PM
(This post was last modified: May-28-2020, 01:40 PM by Harambe.)
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.
Posts: 151
Threads: 3
Joined: Apr 2020
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.
Posts: 16
Threads: 2
Joined: May 2020
(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.
|