2

The code below will allow a user to enter a specified number of people to a list. Each person has three attributes: name, sex, and age. The code should count the number of 'm' characters in the list and count the number of 'f' characters in the list but gives an error when you get to the count lines. How would I fix this issue?

    list1 = []
    person = dict()
    n = int(input("Enter number of elements: "))
    for i in range(0, n):
        print ("Enter information :")
        person[i] = input("Enter name: "), input("Enter sex (m or f): "), input("Enter age: ")
        list1.append(person[i])
        i = i + 1
    print (list1)
    print ("Number of males = " + list1.count('m'))
    print ("Number of females = " + list1.count('f'))

fuwiak
  • 721
  • 1
  • 8
  • 25
SRiley
  • 21
  • 2
  • The list looks like a list of tuples to me. Someone, please correct me if I am wrong. With that in mind, does your code still work? – MatthewS Jan 17 '21 at 20:55
  • https://stackoverflow.com/questions/16013485/counting-the-amount-of-occurrences-in-a-list-of-tuples might be helpful here – MatthewS Jan 17 '21 at 21:00

4 Answers4

1

Try this:

print("Number of males = " + str([i[1] for i in list1].count("m")))
print("Number of females = " + str([i[1] for i in list1].count('f')))
MV912HB
  • 51
  • 1
  • 6
0

Alternatively, you can use counter from the default module «collections»

from collections import Counter

li = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4,]

Counter(li)

This outputs Counter({1: 1, 2: 2, 3: 3, 4: 4}). Counter output is a dict, however, you can update it or select most frequent items.

nad_rom
  • 419
  • 4
  • 9
0

Several issues here:

  • The second print instruction raises the following error TypeError: can only concatenate str (not "int") to str. This is because you are trying to concatenate an integer and a string. For this to work, you should cast the integer to a string like: print ("Number of males = " + str(list1.count('m')))

  • Whatever the user input is, list1.count('m') is always going to be 0 because list1 contains tuples whereas you are looking for the count of a string. You can get a list that only contains the genders via a simple loop: [i[1] for i in list1]

Fixed Code

list1 = []
person = dict()
n = int(input("Enter number of elements: "))
for i in range(0, n):
    print ("Enter information :")
    person[i] = input("Enter name: "), input("Enter sex (m or f): "), input("Enter age: ")
    list1.append(person[i])
    i = i + 1
print (list1)
genders=[i[1] for i in list1]
print("Number of males = " + str(genders.count("m")))
print("Number of females = " + str(genders.count('f')))
Marc Dillar
  • 471
  • 2
  • 9
  • Rather than building a list only to throw it away after you call `count` on it once, you should consider either keeping a reference to it around (since you have two things to count) or counting directly, without the list. `sum(i[1] == 'm' for i in list1)` would work (since `bool` objects are also integers). The `collections.counter` type might also be worth using, with a generator expression, as this would make it easy to get counts for both `'m'` and `'f''`. – Blckknght Jan 17 '21 at 21:07
  • Yes, sure, I just did that to make the logic look clearer. – Marc Dillar Jan 17 '21 at 21:11
0

Use collections.Counter to count objects. I also refactored your code a little to do what you intended:

from collections import Counter
people = []
n = int(input("Enter number of people: "))
for i in range(0, n):
    print ("Enter information:")
    person = (input("Enter name: "),
              input("Enter sex (m or f): "),
              int(input("Enter age: ")))
    people.append(person)
print (people)
c = Counter([p[1] for p in people])
print ("Number of males = " + str(c['m']))
print ("Number of females = " + str(c['f']))

Below are a few comments with suggestions to improve your code:

# make variable name more descriptive, for example users or people:
list1 = []

# no need for defining it here, abd BTW it is a tuple later, not dict:
person = dict()

# elements should be more descriptive, for example users or people:
n = int(input("Enter number of elements: "))
for i in range(0, n):
    print ("Enter information :")
    # Add int conversion to age: 'int(input("Enter age: "))'.
    # Also, person[i] should just one person (a single tuple)
    person[i] = input("Enter name: "), input("Enter sex (m or f): "), input("Enter age: ")
    list1.append(person[i])
    # no need for this, since i is incremented in the statement: 'for i in range(0, n)'
    i = i + 1
print (list1)

# Use collections.Counter instead.
# Also count the second element of each element of the list.
print ("Number of males = " + list1.count('m'))
print ("Number of females = " + list1.count('f'))
Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47