Storing Strings in the list and to sort the list

Hi,
I have written a following program pycharm:

import string
import random

def main():
  generateRandomNumbers()
def generateRandomNumbers():
    for i in range(nameLength):
       x = random.choice(string.ascii_lowercase )
       uname + x
    print (uname)
    list.append(uname)
    list.sort()


size = 0
nameLength=10
uname= ''
if __name__ == "__main__":
    main()

I am getting following error message:

Fl.py

Traceback (most recent call last):
Fl.py", line 19, in
main()
Fl.py", line 5, in main
generateRandomNumbers()
Fl.py", line 11, in generateRandomNumbers
list.append(uname)
TypeError: descriptor ‘append’ requires a ‘list’ object but received a ‘str’

Process finished with exit code 1

Somebody please guide me.

TypeError: descriptor ‘append’ requires a ‘list’ object but received a ‘str’

this basically means youre trying to use a method that an object doesnt have. since list wasnt declared, you cant use append

just add schindlersList = [] to a line right before the for loop

then append and sort with

# eg
schindlersList = []
schindlersList.append('ting')
schindlersList.append('sum')
schindlersList.append('wong')
schindlersList.sort() # ['sum', 'ting', 'wong']

also youre probably going to get an error from this

// uname =  '' # declare your variable before you use it
for i in range(nameLength):
       x = random.choice(string.ascii_lowercase )
       uname + x // uname not declared here. nothing to concat to x
       // uname += x
       // since uname is in memory before the loop started..
       // you concat x to uname 
       // and then uname is reset to the new value after each loop

the python interpreter reads your file from the top down, so you have to declare a variable or function before you can use it

print(uname) # undefined variable

size = 0
nameLength=10
uname= 'johnny'

print(uname) # 'johnny'

if you want to create a list of random lowercase charecter like follows:

['a', 'c', 'f', 'h', 'i', 'l', 'q', 't', 'v', 'x'] 

you need to create a list before for loop i.e. myList=[]
and use myList.append(x) inside for loop .you don’t need uname you can pass x value directly to myList.append() function .
then outside the for loop use myList.sort().

but if you are trying to create a list like follows:

['e', 'ew', 'ewb', 'ewbt', 'ewbtf', 'ewbtfx', 'ewbtfxg', 'ewbtfxgn', 'ewbtfxgng', 'ewbtfxgngq']

you can create uname variable inside generateRandomNumbers() before for loop i.e. uname=""
and use uname += x instead of uname + x.
and use myList.append(uname) inside for loop if you want list of ten items else if you want only 1 item with the 10 character long string inside the array then use myList.append(uname) outside the for loop.

Hi,
I am still getting errors. My code is:

import string
import random


size = 0
nameLength=10
uname = ''
list = []
def main():
  generateRandomNumbers()
def generateRandomNumbers():
    for i in range(nameLength):
       x = random.choice(string.ascii_lowercase)
       uname = uname + x #I have tried uname += x also
       list.append(uname)
    list.sort()



if __name__ == "__main__":
    main()

Errors are:
Fl.py
Traceback (most recent call last):
File “Fl.py”, line 21, in
main()
File “Fl.py”, line 10, in main
generateRandomNumbers()
File “Fl.py”, line 14, in generateRandomNumbers
uname = uname + x #I have tried uname += x also
UnboundLocalError: local variable ‘uname’ referenced before assignment

Process finished with exit code 1

Somebody please guide me.
Zulfi.

you need to put uname variable inside generateRandomNumbers() and before for loop i.e.:

import string
import random

nameLength=10

def main():
  generateRandomNumbers()
def generateRandomNumbers():
    myList = []
    uname = ''
    for i in range(nameLength):
       x = random.choice(string.ascii_lowercase)
       uname += x
       myList.append(uname)
    myList.sort()
    print(myList)


if __name__ == "__main__":
    main()

Hi,
Thanks. Your Solution worked.

God bless you.

I still have question
Why the following declaration does not work?

uname = ''
list = []
def main():

Why is nameLength working?

Zulfi.

it will work if you pass uname as argument in generateRandomNumbers() i.e.

import string
import random
nameLength=10
uname = '';
myList = []
def main():
  generateRandomNumbers(uname)
def generateRandomNumbers(uname):
    
    for i in range(nameLength):
       x = random.choice(string.ascii_lowercase)
       uname += x
       myList.append(uname)
    myList.sort()
    print(myList)


if __name__ == "__main__":
    main()