Write a program that will ask a user to input a word and that will check if the word is a palindrome

QUESTION

PYTHON PROGRAMMING: Write a program that will ask a user to input a word and that will check if the word is a palindrome string. A palindrome is a word that when spelled backwards is the same when read forwards

After the check is performed, if the word is a palindrome, save it in a file called Palindromes.txt, and if the word isn’t a palindrome save it in a separate file called NotAPalindrome.txt

Keep track of how many times a palindrome and non-palindrome word(s) have been entered and graph the results using a pie chart (the # of occurrences of each, two wedges in total),- save the image when the program terminates.

The program should continue to run until the user inputs the word ‘quit’

 

SUMMARY

The user will be asked to enter a number of strings as input until one wants to stop by entering ‘quit’. Each string the user inputs is checked if it is a palindrome string or not. If it is a palindrome string, then the string will be written into the file Palindrome.txt. If it is not a palindrome string, it will be appended to the file NotAPalindrome.txt. In the end, among the strings entered, the number of palindromes and nonPalindromes are counted and then represented on a pie chart.

 

EXPLANATION

A function called palindrome is implemented to which a string is passed as an argument. The string is reversed and is checked with the original string. If equal, then true is returned indicating that it is a palindrome. If not equal, false will be returned. Then continuously a string is taken as input from the user until the user enters quit.

 

If the string entered is a palindrome string, it will be written into the file Palindromes.txt. If not, it will be written to the file NotAPalindrome.txt. The number of palindromes and non-palindromes are counted and using mathplotlib.pyplot library, a pie chart is drawn depicting them.

 

CODE

import matplotlib.pyplot as plt 
""" Method to check if a string is palindrome or not """
def palindrome(s):
    sr=s[::-1]
    if s==sr:
        return True
    else:
        return False
 
""" counting the number of palindromes """
p=0
""" counting the number of strings which are not palindromes """
np=0;
with open("Palindromes.txt","w") as outfile:
    pass 
with open("NotAPalindrome.txt","w") as outfile:
    pass
while True:
    s=input("Enter a string (quit to exit) : ")
    """ to exit if the user enters quit """
    if s=="quit":
        break
    if palindrome(s)==True:
        p+=1
        """ to insert palindrome into the text file """
        with open("Palindromes.txt","a") as outfile:
            outfile.write(s+"\n")
    else:
        np+=1
        """ to insert non palindrome into the text file """
        with open("NotAPalindrome.txt","a") as outfile:
            outfile.write(s+"\n")
 
""" pie chart """
labels='Palindromes','Not_Palindromes'
sizes=[p,np]
colors=['gold','lightskyblue']
plt.pie(sizes,labels=labels,colors=colors,shadow=True,startangle=140,autopct='%1.1f%%')
plt.axis('equal')
plt.show()

 

OUTPUT

 

 

 

Also Read: Write a python code that fulfils the fowllowing requierments

 

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *