首页 > 解决方案 > 对于这段读取它是否为回文的代码,我想用 for 循环替换 [::-1] 并获得相同的功能

问题描述

string = (input("Input your own string: "))
string = string.lower()
if string == string[::-1]:
  print("This is a Palindrome")
else:
  print("This is not a Palindrome")

我想用 for 循环替换 [::-1] 并获得相同的功能

标签: python

解决方案


老实说,我不知道你的真正意思或你的目标是什么,但你可以做类似的事情

for i in range(amount):
    string = (input("Input your own string: "))
    string = string.lower()
    if string == string[::-1]:
        print("This is a Palindrome")
    else:
        print("This is not a Palindrome")

或者

run = True
while run == True:
    string = (input("Input your own string: "))
    string = string.lower()
    if string == string[::-1]:
        print("This is a Palindrome")
    else:
        print("This is not a Palindrome")

推荐阅读