I have a Python 3.x script which only has the following function:
from random import random
def slicer(longitude):
alphabet = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
result = ""
x = 0
for i in range(1, int(longitude)):
x = int(62 * random())
if x == 0:
x += 1
else:
# get a random letter from alphabet, seem this is the problem > (alphabet[x:1]):
string_section = alphabet[x:1]
result = result + string_section
return result
print(str(slicer(10))) # get a 10 digit string
But this code doesn't work, and I'm stuck...
Is s[x:1] permitted, where x is a changing value from a for loop?
If not, is there another method that will accomplish the same result? Thanks for help.
alphabet[x:x+1]) but this is a little misleading. You aren't trying to get a slice that happens to be length 1, you are trying to get a single element. It's probably better to code it that way, so that anyone reading the code sees from your choice that you meant just to get one item. Sincealphabet[x]works, and it doesn't have the pitfalls of requesting a slice that falls outside of the range of indices of the iterable object, it's simpler and easier to understand in this case.