0

I'm somewhat new to python so sorry in advance but I cannot figure out why my unique counter is not increasing with every word that i put into a sequence to count how many palindromes/unique words there are.

user_input = input('Enter word sequence:')

string = user_input.split()

temp = [i[::-1] for i in string]

unique = 0
is_palindrome = 0

for i in range(len(temp)):
      if temp[i] == string[i]:
            is_palindrome += 1
      else:
            unique += 1

print('Sequence contains', unique, 'unique words, where', is_palindrome, 
'words are palindrome.')

it is supposed to count the amount of unique words(not palindromes) and palindromes of a word sequence then output the answer.

1 Answers1

0

As suggested by Allen, using sets and some simplifications:

user_input = input('Enter word sequence: ')
words = set(user_input.lower().split())

palindrome_count = 0
for word in words:
    if word == word[::-1]:
        palindrome_count += 1

out = 'Sequence contains {} unique words, where {} words are palindrome.'
print(out.format(len(words), palindrome_count))

Gabriel Jablonski
  • 854
  • 1
  • 6
  • 17