python - Making a program to detect if a word is a palindrome (ignoring capitalization)
问题描述
I have trouble making this program because I don't know how to make it so the program will ignore capitalization. I used .lower but I think I'm inserting it wrong.
word=input("Give me a word to detect if it is a palindrome")
word=str(word)
if word.lower[::-1]=word :
print("That's a palindrome")
else:
print("Sorry this isn't a palindrome")
解决方案
尝试这个:
word=input("Give me a word to detect if it is a palindrome: ")
word_list = word.split()
for i in word_list:
if i.lower()[::-1]==i.lower() and len(i) > 1:
print("That's a palindrome")
else:
print("Sorry this isn't a palindrome")
这将检测所有回文,如果您只想输入一个单词,请删除 for 循环。
推荐阅读
- python - 将 Django 表单数据保存到用户
- r - 当 barplot 正确绘制所有内容时,ggplot 将所有数据绘制为单列
- c++ - scoped_lock 可以在读取模式下锁定 shared_mutex 吗?
- ruby-on-rails - Rails 5 将环境变量读取为 YAML 文件中的字符串
- vb.net - 如何在打开的窗口外阻止鼠标点击?
- tensorflow - 张量流概率。- 保存和恢复贝叶斯神经网络的检查点
- ruby-on-rails - 由于 Mac OS Mojave 上的错误,无法使用 RVM 安装 ruby
- reactjs - 在 Redux reducer 中设置深度嵌套的对象不会将更新传播到我的组件
- javascript - 在计数之前显示 JavaScript 中的点击计数
- python - 为什么我的解析器不能正确理解“numb=”?