为什么我的代码回文仅适用于单个输入而不适用于多个输入?

发布于 2025-01-18 06:47:21 字数 398 浏览 2 评论 0原文

回文是向前和向后阅读时相同的单词或短语。示例是:“鲍勃”,“ see se”,或“从不奇怪甚至”(忽略空间)。编写一个程序,其输入是单词或短语,并输出输入是否为palindrome。

我只得到这一半。我的代码正在为鲍勃(Bob)工作,并看到。 当输入是“永不奇怪甚至”我的代码不起作用时,它显示的不是回文,而应该是回文。

我在这里做错了什么?

word = str(input())
new = word.replace(" ", "")
new = new[::-1]

if word == new:
    print('{} is a palindrome'.format(word))
else:
    print('{} is not a palindrome'.format(word))

A palindrome is a word or a phrase that is the same when read both forward and backward. Examples are: "bob," "sees," or "never odd or even" (ignoring spaces). Write a program whose input is a word or phrase, and that outputs whether the input is a palindrome.

I'm only getting this half right. my code is working for bob, and sees.
When an input is "never odd or even" my code doesn't work it shows is not a palindrome but it should be a palindrome.

What am I doing wrong here?

word = str(input())
new = word.replace(" ", "")
new = new[::-1]

if word == new:
    print('{} is a palindrome'.format(word))
else:
    print('{} is not a palindrome'.format(word))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

苍景流年 2025-01-25 06:47:21

您正在将Word进行比较,但是要生成new您已删除了所有空格。

You are comparing word with new, but to generate new you had removed all the spaces.

伪心 2025-01-25 06:47:21

这是因为line new = word.replace(“”,“”) - Word与其中的空间保持在一起。您应该在没有空间的情况下制作Word的版本,将其逆转,然后将其与No-Space Word进行比较。

像:

  word = str(input())
  spaceless_word = word.replace(" ", "")
  new = spaceless_word[::-1]

  if spaceless_word == new:
      print('{} is a palindrome'.format(word))
  else:
      print('{} is not a palindrome'.format(word))

That is because of line new = word.replace(" ", "") - the word is kept with the spaces in it. You should make a version of word with no spaces, reverse that, then compare it with the no-spaces word.

Something like:

  word = str(input())
  spaceless_word = word.replace(" ", "")
  new = spaceless_word[::-1]

  if spaceless_word == new:
      print('{} is a palindrome'.format(word))
  else:
      print('{} is not a palindrome'.format(word))
罗罗贝儿 2025-01-25 06:47:21

尝试一下

word = str(input())
word = word.replace(" ", "")
new = word
new = new[::-1]

if word == new:
    print('{} is a palindrome'.format(word))
else:
    print('{} is not a palindrome'.format(word))

try this

word = str(input())
word = word.replace(" ", "")
new = word
new = new[::-1]

if word == new:
    print('{} is a palindrome'.format(word))
else:
    print('{} is not a palindrome'.format(word))
无声无音无过去 2025-01-25 06:47:21

我的答案,有点难看,但我只是不断添加一些东西,直到它是正确的。

inputs=str(input())
inputs_nospace=inputs.replace(' ', '')
inputs_reversed=inputs_nospace
inputs_reversed=inputs_reversed[::-1]
if inputs_reversed==inputs_nospace:
    print (inputs, 'is a palindrome')
else:
    print(inputs, 'is not a palindrome')

My answer, kinda ugly but I just kept adding stuff until it was right.

inputs=str(input())
inputs_nospace=inputs.replace(' ', '')
inputs_reversed=inputs_nospace
inputs_reversed=inputs_reversed[::-1]
if inputs_reversed==inputs_nospace:
    print (inputs, 'is a palindrome')
else:
    print(inputs, 'is not a palindrome')
热风软妹 2025-01-25 06:47:21

尽管其他代码可能有效,但这是我用来确保一切正确的代码。我什至不必使用堆栈溢出。稀有的

user_input = input() # get user input...
bare = user_input.strip().lower().split( ) # did this so even uppercase letters will be able to be palindromes
                                            #though the lab didn't even test that...
text = ''.join(bare) # this removes all space and will be used when compared with reverse_text
reverse_text = text[::-1] # U guessed it this is text reversed
if text != reverse_text: # simple if statement which checks to see if text == reverse_text
    print("not a palindrome:", user_input) # print statement to get full marks on lab
else: # i might be writing too much, but the else statement will only execute if text == reverse_text
    print("palindrome:", user_input) # and print statement to get full marks.

Although other code may work this is the code I used to make sure I got everything correct. I didn't even have to use stack overflow. Rare

user_input = input() # get user input...
bare = user_input.strip().lower().split( ) # did this so even uppercase letters will be able to be palindromes
                                            #though the lab didn't even test that...
text = ''.join(bare) # this removes all space and will be used when compared with reverse_text
reverse_text = text[::-1] # U guessed it this is text reversed
if text != reverse_text: # simple if statement which checks to see if text == reverse_text
    print("not a palindrome:", user_input) # print statement to get full marks on lab
else: # i might be writing too much, but the else statement will only execute if text == reverse_text
    print("palindrome:", user_input) # and print statement to get full marks.
筱武穆 2025-01-25 06:47:21
og_word = str(input())
word = og_word
word = word.replace(" ", "")
new = word
new = new[::-1]

if word == new:
    print(f'palindrome: {og_word}')
else:
    print(f'not a palindrome: {og_word}')
og_word = str(input())
word = og_word
word = word.replace(" ", "")
new = word
new = new[::-1]

if word == new:
    print(f'palindrome: {og_word}')
else:
    print(f'not a palindrome: {og_word}')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文