115k views
0 votes
Write a program palindrome.py that prompts for a sequence of words or numbers on a single line and checks if the entries are palindromes or not. A word or a number is a palindrome if it remains unchanged when reversed. e.g. rotor is a palindrome; but python is not a palindrome. e.g. 737 is a palindrome; but 110 is not a palindrome. The program receives the sequence as input and returns True if an entry is a palindrome or False if an entry is not a palindrome. Print the boolean value on one line. Print the palindromes count on the next line.

1 Answer

2 votes

Answer:

Step-by-step explanation:

The program first asks the user for the sequence of words. Then it splits the sequence into an array of words. Then it loops through the array checking each word to see if it is a palindrome. If it is it prints the word, the boolean value, and adds 1 to the palindrome_count variable. Otherwise it prints the word, false, and moves on to the next word in the list. Finally, it prints out the total value of palindrome_count.

word = input("Enter sequence of words: ")

word_list = word.split(' ')

print(word_list)

palindrome_count = 0

for word in word_list:

print('\\\\')

reverse = word[::-1]

if word == reverse:

print(word, end='\\')

print(True, end="\\")

palindrome_count += 1

else:

print(word, end='\\')

print(False, end='\\')

print("\\\\Number of Palindromes in Sequence: " + str(palindrome_count))

Write a program palindrome.py that prompts for a sequence of words or numbers on a-example-1