Reputation: 21
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'))
Upvotes: 2
Views: 2116
Reputation: 12337
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'))
Upvotes: 0
Reputation: 501
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')))
Upvotes: 0
Reputation: 439
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.
Upvotes: 0
Reputation: 61
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')))
Upvotes: 1