how to count the total number of strings (in the list) used in Python?

Dave Angel d at davea.name
Mon Apr 16 19:36:51 EDT 2012


Did you have a reason to spam us with a second copy of the message after
only 3 minutes?  Usually, you should wait a few days, and then REPLY to
the earlier one, don't leave another with the same subject.  The way you
did it, some people might respond to one, and some to the other,
creating two separate threads.

On 04/16/2012 03:00 PM, Chinesekidz wrote:
> Hello!
>
> I would like to know how to write the program to count the total
> number of strings (in the list) used in Python..

count = len(mylist)

that's all you need to count the number of items in a list.

By the way, don't ever name a variable 'list' since that's the name of
the type.   It would make it hard to create a list from another type,
once you hide the definition.

> for example:
>
> list:['1','2','3','4']
>
> for l in range(4):

l will get the values 0, 1, 2, and 3
(By the way, l is a lousy name for a variable as well, since it looks a
lot like 1)

>      num=input("list:"+list[l]+"(between 1 and 4):")
>      if num.isdigit:
>         tnum=tnum+int(num)

that blows up, since you never gave tnum an initial value.

> print("Total number of list used:",???)
>
> I want the output to be:
>
> list 1 (between 1 and 4): 2
> list 2 (between 1 and 4): 5

The user entered an invalid value, not between 1 and 4.

> list 3 (between 1 and 4): 6
> list 4 (between 1 and 4):5
> list 1 (between 1 and 4):4
How are you going to get that 5th line, when the loop only goes around 4
times?
> Total number of list used: 5
>
> in the case above, it is to show the how many times of strings from
> the list has been used...
>
> I would like to know how to write the code for this...
>
> Thanks in advance
>
>
>
No clue what you're really trying to do.  Maybe you should post the
exact assignment, rather than paraphrasing the teacher.

-- 

DaveA




More information about the Python-list mailing list