"countOut" -- 不在列表中的 while 循环条目不计入扩充赋值 Python 3.6

"countOut" -- while loop entries not from the list are not counted in augmented assignment Python 3.6

这个简单的程序应该计算列表中的条目并打印出有多少 作为不在列表中的计数条目。但出于某种原因,它会将所有条目计为 countIn,无论它们是否来自列表……感谢您的建议!

   fruitsList = ['Apple', 'Banana', 'Grape', 'Peach', 'Mango',
                      'Pear', 'Papaya', 'Plum', 'Grapefruit', 'Cantaloupe']
    countIn=0
    countOut=0

    while True:
        response=input('Enter a fruit name (enter X to exit): ')
        if response.upper() == 'X':
            break
        for response in fruitsList:
            if response in fruitsList:
               countIn += 1
               break
            else:
               countOut += 1
    print('The user entered' , countIn, ' items in the list')
    print('The user entered' , countOut, ' items not in the list')

尝试:

#!user/bin/env python

fruitsList = ['Apple', 'Banana', 'Grape', 'Peach', 'Mango',
                      'Pear', 'Papaya', 'Plum', 'Grapefruit', 'Cantaloupe']
countIn=0
countOut=0

while True:
    response=input('Enter a fruit name (enter X to exit): ')
    if response.upper() == 'X':
        break
    elif response.title() in fruitsList:
        countIn += 1
    else:
        countOut += 1
print('The user entered' , countIn, ' items in the list')
print('The user entered' , countOut, ' items not in the list')

不需要 for 循环。

编辑:我现在还通过为响应字符串添加 title() 函数使其不区分大小写。