如何统计 Python 中的 Y/N 个答案?

How to count Y/N answers in Python?

我一直在尝试使用 y/n 制作一个表格作为每个问题的答案。我一直在想我是否可以做同样的事情 like this,但是对于 y/n 个问题而不是 true/false 个陈述。

这是我一直在处理的代码。

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)

根据代码,我原本希望计算 y 个答案,但最终出现如下错误:

Traceback (most recent call last):
  File [REDACTED], line 14, in <module>
    yCount = sum(booleanList)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

关于如何做到这一点有什么简单但好的想法吗?

您的代码基本上解析为

yCount = sum(["y", "n", "y"])

这将引发此错误:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

所以你必须把元素变成数字来求和:

>>> booleanList = ["y", "n", "y"]
>>> yCount = sum(1 for answer in booleanList if answer == "y")
>>> yCount
2

这会将 booleanList 中的值相加如下:

  • 如果value != "y",则忽略
  • 其他值用1代替求和

如果你想避免像上面生成器表达式那样的高级语言特性(1 for answer in ...),你可以这样写:

for i in range(len(booleanList)):
    booleanList[i] = booleanList[i] == "y"
yCount = sum(booleanList)

这依赖于True在算术表达式中被认为是1

>>> print(True + False + True)
2

如果你想节省一次列表理解并使用比 Bluehorn 的代码更少的内存,你可以试试这个:

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)

与 Bluehorn 的代码相同的警告,如果输入不是 'y',它将被忽略,因此不仅 'n',而且 'y' 或 [=19= 以外的任何其他字符] 将被视为 False.

我会把答案放在一个列表中,然后计算列表中 y 或 no 的总数。

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)

如果您想使用您引用的示例,可以这样做:

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)

这不是推荐的解决方案,但我想提醒您使用 if/else