0

More information

Below is my code. I am in the process of making a small program that finds Palindromes. I want the program to take user input, save it to a variable, and then check for any spaces. If it finds a space to save its index, and then to take it out to check for a Palindromes. Out of curiosity and to further my programming skills, I want to be able to add the space back later while the word is in reverse. For example, nurses run = nursesrun backward and forwards, but I also want to display it backward and add the space back.


word = input("Please enter a word")

storeVal = word.count(" ")
print()

newWord = word.replace(" ", "")
print(newWord)
print()

while True:

  if newWord == "":
    print("Sorry, but you did not enter a word")
    break

  elif newWord == newWord[::-1]:
    #use the index storeVal and add it in the string to put a space back
    print(" is a palidrone")
    break

  elif newWord != newWord[::-1]:
    print("This is not a palidron")
    break

  else:
    print("You have reached an error")

Also, if you have any suggestions for how I can improve my ability to ask this question or a better way I can write this question, please let me know. Thank you for reading.

3
  • Minimal, complete, verifiable example applies here. Show the output you have so far, the output you're trying to get, and include the (not-yet-correct) code you wrote to solve that problem. Commented Sep 19, 2018 at 0:40
  • Why not just print the reverse of the input (i.e. word[::-1]) after checking if it's a palindrome? Commented Sep 19, 2018 at 0:44
  • You have an infinite loop going. while True: and you are not adjusting anything to justify the loop. Remove the while line and move the indentation back up of all lines below the line. You do not need the final else. Convert elif newWord != newWord[::-1]: to else: print("This is not a palidron"). Commented Sep 19, 2018 at 0:46

2 Answers 2

1

Just reverse word instead of reconstructing the modified newWord!

print("{} is a palidrone".format(word[::-1]))

Also storeVal in your example isn't storing the index of the space -- it's storing the number of spaces in your input string.

Sign up to request clarification or add additional context in comments.

1 Comment

I have done that for this assignment, but I was wondering if there was another way to do it
0

As per the other answer, just for displaying you can print word instead of newWord. But if you want to learn how get the original positions of spaces, here are a few hints:

  • word.find(" ") will give you the index of the first space or -1 if none found, you can save it into a list, and than call word.find(" ", space_index + 1) to call the next one, where space_index is the index of last found space, until you get -1 which means no more spaces
  • another, more educated (to my opinion), would to enumerate the string and collect all indexes for spaces in a list using list comprehension (or set, of you don't care about the order).

1 Comment

Thank you for the information. I knew about .set() and thought of using it but wanted something more challenging. I have done word[::-1], but my intentions where to find the space, reverse it, then add the space to show the user that it is in fact a Paldrome. Again, thank you for the hint.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.