使用Python的循环和if语句创建回文检查器 [英] Creating a palindrome checker with Python's loop and if statements

查看:41
本文介绍了使用Python的循环和if语句创建回文检查器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照说明创建回文.我得到了一半的功能,我必须填写空白.我目前无法使循环正常工作.我也不确定如何在不使用 + 或逗号的情况下将字符添加到字符串的开头或结尾.我不认为这是我被要求做的事情.这是说明;

I am trying to follow the instructions to create a palindrome. I am given half a function, and I have to fill in the blanks. I am currently unable to get the loop to work correctly. I am also unsure how to add characters to the beginning or the end of string without using the + or a comma. I do not think that is what I am being asked to do. Here are the instructions;

is_palindrome函数检查字符串是否是回文...如果传递的字符串是回文,则在此函数中填写空白以返回True,否则返回False.

The is_palindrome function checks if a string is a palindrome... Fill in the blanks in this function to return True if the passed string is a palindrome, False if not.

def is_palindrome(input_string):
    # We'll create two strings, to compare them
    new_string = input_string.replace(" ", "")
    reverse_string = input_string.replace(" ", "")
    # Traverse through each letter of the input string
    for word in input_string: # Originally, I was only given the a FOR statement here, I wrote in the rest
        new_string+=word.replace(" ","").upper()
        # Add any non-blank letters to the 
        # end of one string, and to the front
        # of the other string. 

    if ___:
        new_string = ___
        reverse_string = ___
    # # Compare the strings
      if ___:
          return True
          return False

print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True

我已经删除了空白处,并使所有内容都相同.我已经将字符分配给了new_string,但是看起来我应该使用join来添加字符,但是当我执行print语句时,它不会打印任何内容.我不确定如何以相反的顺序添加项目.我什至不确定自己是否步入正轨,因为我不确定IF声明的要求是什么.我认为我应该能够使用循环创建字符串,然后比较两个字符串.

I have removed the empty spaces and made everything the same case. I have assigned the characters to new_string, but it looks like I am supposed to use join to add the characters, but when I do the print statement does not print anything. I am unsure how to add the items in reverse order. I am not even sure if I am on the correct track, because I am unsure what the IF statement is asking. I would think I should be able to use a loop to create the string and then compare the two strings.

另外,有人可以解释一下为什么new_string.join(word)无法打印出任何内容吗?我如何不正确地使用它?

Also, could someone please explain why new_string.join(word) does not print anything out? How am I using it incorrectly?

非常感谢您提供任何可能的帮助.

Thank you very much for any possible assistance.

推荐答案

它对我有用,我尝试并从上面的代码中进行了一些改动,现在可以在下面的代码中使用了.

It worked for me, I have tried and change little bit from above my code it work now with below code.

您可以看到它通过了他们的测试和代码验证,请参阅下面的屏幕截图和代码.

def is_palindrome(input_string):
    # We'll create two strings, to compare them
    new_string = ""
    reverse_string = ""
    # Traverse through each letter of the input string
    for letter in input_string.strip():
        # Add any non-blank letters to the 
        # end of one string, and to the front
        # of the other string. 
        new_string = new_string+letter.replace(" ","")
        reverse_string = letter.replace(" ","")+reverse_string
    # Compare the strings
    if new_string.lower() == reverse_string.lower():
        return True
    return False

print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True

这篇关于使用Python的循环和if语句创建回文检查器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆