Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis0
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)
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.
+ 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?
+ 1
Thanks to you šš
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.
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
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?
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??
0
Thank youuuuu so muchh
0
I can generate 4 seperate letters from alphabets and concatenate them. But i donāt how i would do that
0
Can you set up a for loop?
string = āā
for i in range(4):
string += alphabet[random.randint(0,len(alphabet)-1)]
alphabet = string
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
0
I meant random.randrange
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
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.
0
alphabet[random.randint(0,len(alphabet)-1)] this might work?
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)
0
If youāve got what you need, great! Congrats!