Ask coding questions

← Back to all posts
Help with random.sample input [Python]
Bookie0 (3694)

Hi there!

So i have this code:

import random

s = "a[email protected]#$%^&*()?"
passlen = 8
p =  "".join(random.sample(s,passlen ))
print (p)

that generates a random password with length of 8.

however, if i want to change passlen to an input (see repl below), it gives an error.

Anyone know why and how to fix it?

Any help much appreciated!

thanks in advance! ;)

Answered by Coder100 (6992) [earned 5 cycles]
View Answer
Coder100 (6992)

"".join(random.sample(s, int(passlen)))
smh how dare you not do this

Bookie0 (3694)

ah ok thankssssss a millioooooon @Coder100

Coder100 (6992)

np!! its a smol mistake we all make it @Bookie0

DynamicSquid (3294)

@Coder100 I was gonna answer the question lol