使用 Python 的循环和 if 语句创建回文检查器
Creating a palindrome checker with Python's loop and if statements
我正在尝试按照说明创建回文。我得到了一半的功能,我必须填空。我目前无法让循环正常工作。我也不确定如何在不使用 + 或逗号的情况下将字符添加到字符串的开头或结尾。我不认为这是我被要求做的事情。
这是说明;
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 来添加字符,但是当我这样做时,打印语句不会打印任何内容。我不确定如何以相反的顺序添加项目。
我什至不确定我是否在正确的轨道上,因为我不确定 IF 语句在问什么。我认为我应该能够使用循环来创建字符串,然后比较这两个字符串。
此外,有人可以解释一下为什么 new_string.join(word) 没有打印出任何东西吗?我怎么用错了?
非常感谢您的帮助。
def is_palindrome(input_string):
new_string = input_string.replace(" ", "").lower()
reverse_string = input_string.replace(" ", "").lower()[::-1]
if new_string == reverse_string:
return True
return False
> This should do it.
我写了这个程序,
它 returns 也是预期的结果,但它被拒绝了。
不知道他们到底在内部测试什么来验证代码及其正确性。
我的实现:
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.
if letter !=' ':
new_string = new_string+letter
reverse_string = letter+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
如果有人知道我在这里到底缺少什么?
它对我有用,我已经尝试从我的代码上面稍微改变一下它现在可以使用下面的代码。
你可以看到它通过了他们的测试和代码验证,参考下面的截图和代码。
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
对我有用,这段代码是系统期望看到的。
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 word in input_string:
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
word = word.lower()
if word != " ":
new_string = new_string + word
reverse_string = word + reverse_string
# Compare the strings
if new_string == reverse_string:
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
is_palindrome function
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 char in input_string:
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
if char !=" ":
new_string +=char.lower()
reverse_string =char.lower()+reverse_string
# Compare the strings
if new_string==reverse_string:
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
这很简单。主要方法是
- 将给定的字符串转换为小写
- 检查白色 space“”
插入新字符串
new_string = new_string+i
reverse_string = i+reverse_string
检查新字符串是否== reverse_string
代码:
# We'll create two strings, to compare them
input_string = input_string.lower()
new_string = ""
reverse_string = ""
# Traverse through each letter of the input string
for i in input_string:
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
if i !=" ":
new_string = new_string+i
reverse_string = i+reverse_string
# Compare the strings
if new_string == reverse_string:
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
输出
True
False
True
这对我有用:
def is_palindrome(input_string):
# We'll create two strings, to compare them
new_string = input_string.replace(" ", "")
new_string = new_string.lower()
reverse_string = new_string[::-1]
reverse_string = reverse_string.lower()
# Traverse through each letter of the input string
if new_string == reverse_string:
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
def is_palindrome(input_string):
# We'll create two strings, to compare them
new_string = ""
reverse_string = ""
# Traverse througenter code hereh each letter of the input string
for letter in input_string.strip().lower():
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
if letter!=" ":
new_string = new_string+letter
reverse_string = letter+reverse_string
# Compare the strings
if new_string==reverse_string:
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
def is_palindrome(input_string):
new_string = ""
reverse_string = ""
for word in input_string:
if word != " ":
new_string = new_string.strip().lower() + word
reverse_string = word + reverse_string.strip().lower()
# Compare the strings
if new_string == reverse_string:
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
strip()
或 replace()
函数在这种情况下不一定有用。
strip()
方法不会删除短语中每个单词之间的空格,即第一种情况 'Never Odd or Even'。
因此,一个简单的 if 语句就可以完成这项工作。
不要忘记将字符串转换为 upper()
或 lower()
大小写。
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.lower():
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
if letter!=" ":
new_string += letter
reverse_string = letter + reverse_string
# Compare the strings
if new_string == reverse_string:
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
我有一些空闲时间所以我尝试手动索引并且 它对我有用 :D
def is_palindrome(input_string):
input_string = input_string.lower().replace(" ", "") '''make the string have
lower letter remove the withspace'''
new_string = ""
reverse_string = ""
for n in range(len(input_string)):
new_string += input_string[n]
reverse_string += input_string[(len(input_string)-(1+n))] '''manually make string
from behind with indexing.'''
if new_string == reverse_string:
return True
return False
def is_palindrome(input_string):
new_string = input_string.lower()
no_space = new_string.replace(" ","")
reverse_string =new_string.replace(" ","")[::-1]
if reverse_string==no_space:
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
我正在尝试按照说明创建回文。我得到了一半的功能,我必须填空。我目前无法让循环正常工作。我也不确定如何在不使用 + 或逗号的情况下将字符添加到字符串的开头或结尾。我不认为这是我被要求做的事情。 这是说明;
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 来添加字符,但是当我这样做时,打印语句不会打印任何内容。我不确定如何以相反的顺序添加项目。 我什至不确定我是否在正确的轨道上,因为我不确定 IF 语句在问什么。我认为我应该能够使用循环来创建字符串,然后比较这两个字符串。
此外,有人可以解释一下为什么 new_string.join(word) 没有打印出任何东西吗?我怎么用错了?
非常感谢您的帮助。
def is_palindrome(input_string):
new_string = input_string.replace(" ", "").lower()
reverse_string = input_string.replace(" ", "").lower()[::-1]
if new_string == reverse_string:
return True
return False
> This should do it.
我写了这个程序,
它 returns 也是预期的结果,但它被拒绝了。
不知道他们到底在内部测试什么来验证代码及其正确性。
我的实现:
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.
if letter !=' ':
new_string = new_string+letter
reverse_string = letter+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
如果有人知道我在这里到底缺少什么?
它对我有用,我已经尝试从我的代码上面稍微改变一下它现在可以使用下面的代码。
你可以看到它通过了他们的测试和代码验证,参考下面的截图和代码。
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
对我有用,这段代码是系统期望看到的。
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 word in input_string:
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
word = word.lower()
if word != " ":
new_string = new_string + word
reverse_string = word + reverse_string
# Compare the strings
if new_string == reverse_string:
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
is_palindrome function
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 char in input_string:
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
if char !=" ":
new_string +=char.lower()
reverse_string =char.lower()+reverse_string
# Compare the strings
if new_string==reverse_string:
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
这很简单。主要方法是
- 将给定的字符串转换为小写
- 检查白色 space“”
插入新字符串
new_string = new_string+i reverse_string = i+reverse_string
检查新字符串是否== reverse_string
代码:
# We'll create two strings, to compare them
input_string = input_string.lower()
new_string = ""
reverse_string = ""
# Traverse through each letter of the input string
for i in input_string:
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
if i !=" ":
new_string = new_string+i
reverse_string = i+reverse_string
# Compare the strings
if new_string == reverse_string:
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
输出
True
False
True
这对我有用:
def is_palindrome(input_string):
# We'll create two strings, to compare them
new_string = input_string.replace(" ", "")
new_string = new_string.lower()
reverse_string = new_string[::-1]
reverse_string = reverse_string.lower()
# Traverse through each letter of the input string
if new_string == reverse_string:
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
def is_palindrome(input_string):
# We'll create two strings, to compare them
new_string = ""
reverse_string = ""
# Traverse througenter code hereh each letter of the input string
for letter in input_string.strip().lower():
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
if letter!=" ":
new_string = new_string+letter
reverse_string = letter+reverse_string
# Compare the strings
if new_string==reverse_string:
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
def is_palindrome(input_string):
new_string = ""
reverse_string = ""
for word in input_string:
if word != " ":
new_string = new_string.strip().lower() + word
reverse_string = word + reverse_string.strip().lower()
# Compare the strings
if new_string == reverse_string:
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
strip()
或 replace()
函数在这种情况下不一定有用。
strip()
方法不会删除短语中每个单词之间的空格,即第一种情况 'Never Odd or Even'。
因此,一个简单的 if 语句就可以完成这项工作。
不要忘记将字符串转换为 upper()
或 lower()
大小写。
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.lower():
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
if letter!=" ":
new_string += letter
reverse_string = letter + reverse_string
# Compare the strings
if new_string == reverse_string:
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
我有一些空闲时间所以我尝试手动索引并且 它对我有用 :D
def is_palindrome(input_string):
input_string = input_string.lower().replace(" ", "") '''make the string have
lower letter remove the withspace'''
new_string = ""
reverse_string = ""
for n in range(len(input_string)):
new_string += input_string[n]
reverse_string += input_string[(len(input_string)-(1+n))] '''manually make string
from behind with indexing.'''
if new_string == reverse_string:
return True
return False
def is_palindrome(input_string):
new_string = input_string.lower()
no_space = new_string.replace(" ","")
reverse_string =new_string.replace(" ","")[::-1]
if reverse_string==no_space:
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