'How to count Y/N answers in Python?
I've been trying to make a form using y/n as answers for each of the questions. I've been wondering if I could do the same thing like this, but for y/n questions instead of true/false statements.
Here's the code I've been working on.
print('Are You Ready for Omicron?')
print('Here\'s a test to make sure you\'re ready')
print('Note: please answer in y or n')
print('')
faceMask = input('Do you have a face mask? ')
faceShield = input('Do you have a face shield? ')
alcohol = input('Do you have alcohol in hand? ')
booleanList = [faceMask, faceShield, alcohol]
yCount = sum(booleanList)
print(yCount)
Based on the code, I was expecting to count the y answers but ended up with an error like this:
Traceback (most recent call last):
File [REDACTED], line 14, in <module>
yCount = sum(booleanList)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Any simple yet good ideas on how to do this?
Solution 1:[1]
If you want to work with the example you cited, you could do this:
print('Are You Ready for Omicron?')
print('Here\'s a test to make sure you\'re ready')
print('Note: please answer in y or n')
print('')
faceMask = input('Do you have a face mask? ')
if faceMask == "y":
faceMask = True
else:
faceMask = False
faceShield = input('Do you have a face shield? ')
if faceShield == "y":
faceShield = True
else:
faceShield = False
alcohol = input('Do you have alcohol in hand? ')
if alcohol == "y":
alcohol = True
else:
alcohol = False
booleanList = [faceMask, faceShield, alcohol]
yCount = sum(booleanList)
print(yCount)
This is not a recommended solution but I wanted to remind you to use if/else
Solution 2:[2]
Your code basically resolves to
yCount = sum(["y", "n", "y"])
This will raise this error:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
So you have to turn the elements into numbers to sum them:
>>> booleanList = ["y", "n", "y"]
>>> yCount = sum(1 for answer in booleanList if answer == "y")
>>> yCount
2
This will sum the values in booleanList as follows:
- if
value != "y", it is ignored - other values are summed by replacing them with 1
If you want to avoid higher language features like the generator expression above (1 for answer in ...), you can write this as follows:
for i in range(len(booleanList)):
booleanList[i] = booleanList[i] == "y"
yCount = sum(booleanList)
This relies on the fact that True is considered to be 1 in arithmetic expressions:
>>> print(True + False + True)
2
Solution 3:[3]
If you want to save one list comprehension and use less memory than Bluehorn's code, you could try this:
print('Are You Ready for Omicron?')
print('Here\'s a test to make sure you\'re ready')
print('Note: please answer in y or n')
print('')
faceMask = input('Do you have a face mask? ').lower() == 'y'
faceShield = input('Do you have a face shield? ').lower() == 'y'
alcohol = input('Do you have alcohol in hand? ').lower() == 'y'
booleanList = [faceMask, faceShield, alcohol]
yCount = sum(booleanList)
print(yCount)
Same caveat as Bluehorn's code, if input is not 'y', it will be ignored and hence not only 'n' but any other character other than 'y' or 'Y' will be considered as False.
Solution 4:[4]
I would put the awnsers in a list then count the totalt of y or no in the list.
print('Are You Ready for Omicron?')
print('Here\'s a test to make sure you\'re ready')
print('Note: please answer in y or n')
print('')
awnser_list = [] #empty list to fill
faceMask = input('Do you have a face mask? ')
faceMask = faceMask.lower() #make sure that all awnsers is in lower case.
faceShield = input('Do you have a face shield? ')
faceShield = faceShield.lower()
alcohol = input('Do you have alcohol in hand? ')
alcohol = alcohol.lower()
awnser_list.append(faceMask) #append the awnsers to the list
awnser_list.append(faceShield)
awnser_list.append(alcohol)
result = awnser_list.count("y") #count totalt of y in the list
print('Total of y: ',result)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ProgrX |
| Solution 2 | Bluehorn |
| Solution 3 | |
| Solution 4 | Dharman |
