[Python] Checking for a palindrome, using for-loop in Python and print output ONLY ONCE

Discussão em 'Python' iniciado por Stack, Junho 21, 2025.

  1. Stack

    Stack Membro Participativo

    I am fairly new to programming, so I hope you can help me.

    I want to check if a input string is a palindrome. The palindrome-checker is case-insensitive.

    Here is what I got so far:

    # input word
    word = input("Enter a word: ")

    # make it case-INsensitive
    word = word.lower()

    # we also need the length of word to iterate over its range
    length_word = int(len(word))

    ### use a for-loop
    for letter in range(len(word)):
    if word[-length_word] == word[-1]:
    print(word, "is a palindrome.")

    # if it doesn't match, its not a palindrome, print message
    else:
    print(word, "is not a palindrome.")


    What bothers me is that it prints the phrase "is a palindrome." everytime. How can I fix it so it will only print it once if the word is a palindrome?

    Thank you so much in advance!

    Continue reading...

Compartilhe esta Página