如何在Python中创建循环?

时间:2016-05-10 16:03:18

标签: python

如何将此代码放入循环中?

EnterWord = input ("Please Enter your Word here: ")
Reverse = EnterWord [::-1]

if EnterWord.lower() == Reverse.lower():
    print ("Hurrah!!! You have just found a Palindrome")

else:
    print ("Oh, Come on! you can do this man...")

我该怎么做?

3 个答案:

答案 0 :(得分:2)

如果你想无限期地循环它,只需将True添加到程序中:

while True:
    EnterWord = input ("Please Enter your Word here: ")
    Reverse = EnterWord [::-1]

    if EnterWord.lower() == Reverse.lower():
         print ("Hurrah!!! You have just found a Palindrome")
    else:
         print ("Oh, Come on! you can do this man...")

答案 1 :(得分:1)

试试这个

while True:
   EnterWord = input ("Please Enter your Word here: ")
   Reverse = EnterWord [::-1]

   if EnterWord.lower() == Reverse.lower():
       print ("Hurrah!!! You have just found a Palindrome")
   else: 
       print ("Oh, Come on! you can do this man...")

答案 2 :(得分:-3)

我认为这就是你要找的东西:

EnterWord = input ("Please Enter your Word here: ")
Reverse = EnterWord [::-1]

while EnterWord.lower() != Reverse.lower():
    EnterWord = input ("Please Enter your Word here: ")
    Reverse = EnterWord [::-1]
    print ("Oh, Come on! you can do this man...")

print ("Hurrah!!! You have just found a Palindrome")