0

I need to make this program get the users input from individually asking for each word, then putting it into a list and displaying the list as a sentence and then it displays how many words are in the sentence. I just can't seem to figure out how to get out of this While loop. When I put in anything besides "y" or "Y" in the input box it always repeats the input for a word.

Thanks in advance.

list1=[]
again = True
count = 0

choose = str(input('Enter another word? (y or Y for yes, n or N no): '))
while choose == 'y' or 'Y':
    again = True
    if again == True:
        words = input('Enter a word: ')
        choose = str(input('Enter another word? (y or Y for yes): '))

else:
    again = False

if list1 !=' ':
    readList = list1
    count += 1
    print(readList.capitalize(), '.', '\n')

print('Your sentence has', count, 'words in it.')
  • 1
    There are a lot of problems here. The first one is that `choose == 'y' or 'Y'` means `(choose == 'y') or 'Y'`, which is either going to be `True` or the letter `'Y'`, which are both truthy. But beyond that, you're testing `again` when you've just set it to `True` so the test can't be useful, and all you do with the test anyway is assign `again` the value it already has, so… – abarnert Mar 11 '18 at 23:24
  • 1
    with current formatting your code shouldn't compile. The `else` has bad indentation. – triandicAnt Mar 11 '18 at 23:25
  • You'll want to set the loop to `while choose == 'y' or choose == 'Y'` and then indent the rest of you code. – Tony Mar 11 '18 at 23:27
  • @triandicAnt: No, it's perfectly legal—although it probably doesn't mean what he intended. [`while` statements have `else` clauses](https://docs.python.org/3/reference/compound_stmts.html#the-while-statement). – abarnert Mar 11 '18 at 23:27

1 Answers1

0

'Y' will always return true

Change to this:

while choose == 'y' or choose == 'Y':

or

while choose.lower() == 'y'
Ruslan L.
  • 436
  • 3
  • 7