Python Forum
Help with Python - Palindromes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Python - Palindromes
#1
Question 
Hello everyone,

I have a problem with getting the result of my program Confused .

This is a task on palindromes.

STATEMENT
Write a program which reads a string and prints all palindromes, which can be obtained by removing of some characters of the string. The resulting palindromes do not need to be English words!
Restrictions:
  • do not print palindromes of the length 1 (one-letter palindromes)
  • each palindrome is printed exactly once
I have written this code but I do not understand why it does not print the final result Cry .

If anyone can find the problem and fix it, I appreciate it Heart .

CODE
def check_palin(word):
    if word==word[::-1]:
            return True
    else:
        return False
 
def all_palindromes(string):
   result=list()
   st1=""
 
   for i in range(0,len(string)):
        st1 += string[i]
        result.append(string[i])
        for j in range(i+1,len(string)):
           st1+=string[j]
           if check_palin(st1)==True:
                result.append(st1)
        st1=""
    
   return list(set(result))

w = input("Write any string to find all palindromes: ")
all_palindromes(w)
#2
You don't have a call to print anywhere.

Also, the if and else in check_palin are redundant.
#3
(Apr-04-2021, 02:27 PM)ndc85430 Wrote: You don't have a call to print anywhere.

Also, the if and else in check_palin are redundant.


I need to call the function to can see the result if it works good or not :S I tried with
all_palindromes(w)
but still not bring the result on the screen and really I dont know why.

This if and else are just to check if the 1st string that I write as input is or not a palindrome, I don't know if is it the best option to do that but I am really new on python.
#4
Well, you aren't printing anything are you? Why would you expect anything else to happen? All you do is call the function and throw its return value away.
#5
Please, keep the discussion here https://python-forum.io/Thread-Palindrom...#pid140162
I close this thread now.
STOP creating new threads!
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs



Possibly Related Threads…
Thread Author Replies Views Last Post
  Palindromes in Python karansingh 2 2,304 May-08-2019, 07:17 AM
Last Post: DeaD_EyE
  What are Palindromes in Python? HarshaliPatel 5 3,791 Dec-11-2018, 07:24 AM
Last Post: himanibansal

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020