0

I would like to create a user input for a function of mine. So this function of mine is a boolean function. It is used to test whether a word is a palindrome or not by giving an output True or False.

I want to create a separate function (user input) where a person can type in the phrase that they want to find out if its a palindrome or not and it should print out the words "The phrase you entered, XXXXXX is a palindrome" when output is true and when output is false it should go "The phrase you entered, XXXXXX is not a palindrome".

My code as below:

def palindrome(phrase):
    len_phrase = len(phrase)//2
    print("len_phrase",len_phrase)
    for i in range(len_phrase):
        print(phrase[i],phrase[-i-1])
        if phrase[i] != phrase[-i-1]:
            return False
    return True     
4
  • Assuming you just want text input via the command line, you should look into the input function. w3schools.com/python/ref_func_input.asp Commented Nov 29, 2021 at 10:44
  • No I want a person to be able to enter a phrase themselves and once they do it should start printing out "The phrase you entered, XXXXXX is a palindrome" when output is true and when output is false it should go "The phrase you entered, XXXXXX is not a palindrome". I just cant seem to link it to my function Commented Nov 29, 2021 at 11:19
  • You get user input with the "input" function, pass that to your palindrome function and test the return. Commented Nov 29, 2021 at 11:35
  • Jeff gave a nice answer. I would add a few things about the design. 1) remove print statements from your function. They are redundant in that logic. A clean function should do one thing. 2) I think that if you call the function is_palindrome, that would be more readable and explicit (see The Zen of Python, import this). Commented Nov 29, 2021 at 13:44

1 Answer 1

2

You would use input to request a string from the user; something like this :

def palindrome(phrase):
    [...]
    
user_input = input("Enter some text:")
  
if palindrome(user_input):
    print(f'{user_input} is a palindrome')
else:
    print(f'{user_input} is not a palindrome')
Sign up to request clarification or add additional context in comments.

Comments

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.