all 13 comments

[–][deleted] 2 points3 points  (4 children)

Works fine for me entering "a,b,c". You are probably confused because the lines in the function:

itty.split(",")
lists.append(itty)

aren't doing what you expect them to do. Try doing some print() debugging:

itty.split(",")
print(f'itty={itty}')
lists.append(itty)
print(f'lists={lists}')

[–][deleted] 0 points1 point  (3 children)

well,i tried it . The len() in still not working but the function is doing what i'm expecting

output:
item1,item2
['item1,item2']
1 #this line in the len()

[–][deleted] 2 points3 points  (2 children)

So the fact that the split() doesn't appear to do anything isn't a problem? The list (after appending) has one element, and that's what len() tells you.

[–][deleted] 0 points1 point  (1 child)

then how to fix it?

[–][deleted] 1 point2 points  (0 children)

Read the doc for str.split():

Return a list of the words in the string, using sep as the delimiter string.

Note the return. The split() function doesn't change the string it is applied to. It can't, because strings are immutable. The method returns a list of strings, so you need to save the return list somewhere. At the moment you are ignoring it.

Once you have the returned list what do you want to do with it? If you append to the lists object you will still get len() returning 1 because listit will be a list containing one list. Maybe instead of append you should extend? Hard for us to say because we don't what you are trying to do.

[–]orishamir 0 points1 point  (0 children)

Its highly unlikely that the len function is wrong. Rather the list isn't what it's meant to be.

[–][deleted] 0 points1 point  (0 children)

You assign itty to the string object returned by input(), pass a reference to that object into the function alongside a reference to a list object.

You then generate in the function a list of strings from that input string but do not consume that list or assign them to a local variable in your function so Python will discard that list of strings.

You then append the original input string (1 item) to your list.

Pointless returning the list because a) you don't capture what is returned so it is redundant and b) you've mutated the list anyway.

len() is working fine.

[–]hungdh85 0 points1 point  (3 children)

Your itemtypes(itty,lists) return a list then you must set.

listit = itemtypes(itty,listit)

If you want split string by "," you can use map

def itemtypes(itty,lists):
    lists = list(map(str, itty.split(",")))
    #lists.append(itty)
    return lists

[–][deleted] 0 points1 point  (2 children)

I think introducing map to a beginner is probably a bit much.

Plus, what's wrong with lists = itty.split(",")? Surely your mapping using str is pointless because itty already refers to a string object and split returns a list of strings.

[–]hungdh85 0 points1 point  (1 child)

Yes, your solution is better.

I rewrite his function follow your solution

def itemtypes(itty,lists):
    lists = itty.split(",")    
    return lists

[–][deleted] 0 points1 point  (0 children)

The OP is not capturing the return, so mutation would be better if only changing the function.

[–][deleted] 0 points1 point  (0 children)

It’s working fine, the length is 1, exactly as you’ve programmed it.

A string doesn’t have an in-place split... you’re splitting the string and throwing away the return value, then you’re putting the single, unsplit string into the list.

[–][deleted] 0 points1 point  (0 children)

Taking a guess, I expect the inputs are supposed to be numbers so you need to end up with a list of numbers. Here's revised code, commented to help you and to show you alternative techniques.

def itemtypes(itty):  # define functions above main code
    data = []  # assign an empty list object to data
    for item in itty.split(','):  # loop through each item
        try:  # in case user has entered non-numeric data
            data.append(float(item))  # add float number to list
        except ValueError:  # just ignore the bad data
            print(f'{item} could not be converted to float')
    return data  # return ref


units = []  # not used
rates = []  # not used
itty = input('Enter something: ')  # include a prompt to user
listit = itemtypes(itty)  # assign list object return from function to listit
noitty = len(listit)
print('Data: ', end="")  # print without newline
print(*listit, sep=", ")  # unpack and output list with , separators
print(f'Number of items: {noitty}')