how would you generate an 8 character password with 4 random alphabet and 4 random number using slicing and concatenation. | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

how would you generate an 8 character password with 4 random alphabet and 4 random number using slicing and concatenation.

alphabet =ā€œABCDEFGHIJKLMNOPQRSTUVQXYZabcdefghijklmnopqrstuvwxyzā€ random_number = (1, 999)

25th Mar 2019, 12:34 PM
banna101
17 Respostas
+ 1
Which random functions are you allowed to use? I donā€™t see any way of choosing random letters just from slicing your alphabet string.
25th Mar 2019, 5:41 PM
Russ
Russ - avatar
+ 1
I think Iā€™ve figured it out using first_letter = alphabet[random.randint(0,len(alphabet)-1)] To get 4 i copied and paste and change the name to second_letter, third and fourth. It seems to work?
25th Mar 2019, 7:25 PM
banna101
+ 1
Thanks to you šŸ™šŸ™
25th Mar 2019, 7:36 PM
banna101
0
Any attempt of your own? Youā€™re far more likely to get assistance if you show us that youā€™ve made an attempt to solve it yourself.
25th Mar 2019, 2:54 PM
Russ
Russ - avatar
0
import random alphabet = ā€œABCDEFGHIJKLMNOPQRSTUVQXYZabcdefghijklmnopqrstuvwxyzā€ rand_index = random.randrange (0, len(alphabet)) first_slice = alphabet[:rand_index] alphabet = first_slice random_number = random.randrange(1, 999) print(ā€œYour password is: ā€œ, alphabet + str(random_number)) output should be 4charcters of alphabet and 4 characters of random number with extend of zero So for example tziy0098 or gtjs0999 However the real result is abc715 I canā€™t figure out how to get only four random alphabet And how to zero extend the number so i generate four characters of random number. Please help
25th Mar 2019, 5:10 PM
banna101
0
For the letters: alphabet = list(first_slice) random.shuffle(alphabet) alphabet = ā€œā€.join(alphabet[:4]) For numbers: random_number = (ā€œ0000ā€ + str(random_number))[-4:] Does this satisfy your criteria?
25th Mar 2019, 5:34 PM
Russ
Russ - avatar
0
Thank you and Instead for alphabet we can only use slicing method. Like removing random characters but i wanna have 4 random alphabet everytime how would i do that??
25th Mar 2019, 5:37 PM
banna101
0
Thank youuuuu so muchh
25th Mar 2019, 5:37 PM
banna101
0
I can generate 4 seperate letters from alphabets and concatenate them. But i donā€™t how i would do that
25th Mar 2019, 5:47 PM
banna101
0
Can you set up a for loop? string = ā€œā€ for i in range(4): string += alphabet[random.randint(0,len(alphabet)-1)] alphabet = string
25th Mar 2019, 5:50 PM
Russ
Russ - avatar
0
We arenā€™t allowed to use loop yet. So far i can only generate 4 random alphabets By generating rand_index and slicing them
25th Mar 2019, 5:56 PM
banna101
0
I meant random.randrange
25th Mar 2019, 5:58 PM
banna101
0
If used random.randrange i keep getting more than 4 alphabets or less than 4 alphabets. So maybe itā€™s not random.range Maybe slicing them but how would i slice to get one random character at a time and concatenate them
25th Mar 2019, 6:00 PM
banna101
0
Unless Iā€™m missing something, there doesnā€™t seem to be a way of doing this by slicing without shuffling the order of your alphabet. If you canā€™t use a loop, canā€™t you just use the line I put inside the loop 4 times? I still feel like Iā€™m in the dark about what random functions can/canā€™t be used.
25th Mar 2019, 7:05 PM
Russ
Russ - avatar
0
alphabet[random.randint(0,len(alphabet)-1)] this might work?
25th Mar 2019, 7:07 PM
banna101
0
Hello i have an example name = ā€œapple pieā€ first_part = name[0:4] ā€”->instead how would you put random instead of giving the actual location? print(first_part)
25th Mar 2019, 7:17 PM
banna101
0
If youā€™ve got what you need, great! Congrats!
25th Mar 2019, 7:34 PM
Russ
Russ - avatar