string - slicing dynamically in python -


I have a Python 3.x script that only the following tasks:

  random Import Random DEF slicer (longitude): alphabet = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" results = "" x = 0 in range (1, integer (longitude)): x = integer (62 * random ()) if x == 0: X + = 1 Other: Receive random letters from # alphabet, this is the problem & gt; (Alphabet [x: 1]): string_section = alphabet [x: 1] result = result + string_section return result print (STR (slicer (10))) # string of 10 digits  get 

However, this code does not work, and I'm stuck

what s [x: 1] is allowed, where x loop A changing value?

If not, then what is the other way that will accomplish the same result? Thanks for the help.

The problem is that the x is lower than almost always 1, then < code> [x: 1] is a meaningless range I just do:

  alphabet = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" alphabet [32: 1]  

I think python returns can not see anything. If your script does not add anything to the constant Result - alphabet from that position you do need to take the alphabet [x] and the If the alphabet is [0] is a , then you do not need to add 1 if x is 0. (In your implementation, you are never likely to be twice than any other form of "A" and "B"). Random Import From random def slicer (longitude)

 : alphabet = "abcdefghijklmnopqrstuvwxyz0123456789 ABCDEFGHILMnOPCRSTUVXZ" for result = "" x = 0 I (1, int (longitude)) in the range: x = int (62 * random ()) String_section = alphabet [x] result = result + string_section return result print (STR (slicer (10))) # string of 10 digits  get 

but .. ..

If you want to make sure you repeat the letters If not, then you just random.sample () :

  import using random-def Slicer (longitude): alphabet = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" Result = Random . Sample (alphabet, longitude) # Join To print the results ( ''. (Slojhr (10)) has compiled a list, use. / Code> 

Comments