如何在Python中计算Y/N答案



我一直在尝试用y/n作为每个问题的答案来制作一个表格。我一直在想我是否也可以这样做,但对于y/n问题,而不是对/错的陈述。

这是我一直在研究的代码。

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'

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

如果你想保存一个列表的理解,并且使用比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"或"y"之外的任何其他字符都将被视为False

您的代码基本上解析为

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

如果你想使用你引用的例子,你可以这样做:

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

我会把awnsers放在一个列表中,然后计算列表中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)

最新更新