0

This simple program should count entries from the list and print how many there were as well as counting entries that are not from the list. But for some reason it counts all entries as countIn despite if they are from the list or not... Appreciate your suggestions!

   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')
Rob
  • 143
  • 10
Rish
  • 73
  • 7
  • How many attempts does the player have? – Reblochon Masque Oct 04 '17 at 02:30
  • as many as wanted really. – Rish Oct 04 '17 at 02:31
  • It should look like this : Enter a fruit (x to exit): apple Enter a fruit (x to exit): grape Enter a fruit (x to exit): fly Enter a fruit (x to exit): x The user entered 2 items in the list The user entered 1 items not in the list – Rish Oct 04 '17 at 02:32
  • 1
    You don't even need to loop through the list, you can just have your `if response in fruitsList:` and that will do just fine. You are using the same variable `response` for your input and for your iterator. – kstullich Oct 04 '17 at 02:35
  • @kstullich I wish I can skip that. Our prof specifically asked for while loop and use augmented assignment to count entries in and out of the list. I am having a hardest time with Python. Never have such issues with Java :)) – Rish Oct 04 '17 at 02:40
  • @Rish you would still be using augmented assignment if you took out the for loop.....take a look at the answer posted below it's what I would do. – kstullich Oct 04 '17 at 02:44
  • @kstullich looks like it works now! I am so confused though why it wouldn't do so by the book... – Rish Oct 04 '17 at 02:51
  • @Rish I am guessing cause you used the same variable name (`response`) for your iterator and `input()`. But also you then don't compare the iterator to the input given. You would need to say `if response == fruitsList:`. Since you are iterating through the whole list. – kstullich Oct 04 '17 at 02:53
  • @kstullich ah I see. I'm gonna try "if response==fruitList" as well.. see how it goes. Many thanks! – Rish Oct 04 '17 at 02:59

1 Answers1

3

Try:

#!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')

There is no need for a for-loop.

EDIT: I also made it case-insensitive now by adding the title() function for the response string.

Rob
  • 143
  • 10
  • awwww magic! Thanks a bunch! It maybe a question over the top, but does anyone know why it didn't work before? This is how my prof wrote it really and he is great... usually leaves some spaces for us to think through though... – Rish Oct 04 '17 at 02:47
  • 1
    With the for-loop you were walking through the fruit list and assigning the variable response, even though response was already assigned to the input. – Rob Oct 04 '17 at 02:50