2013-01-24 63 views
2

我只是一个Python新手。 我有这个算法看看是不是一个词是回文。在我的算法中实现raw_input

def isPalindrome(s): 

    def toChars(s): 
     s = s.lower() 
     ans = '' 
     for c in s: 
      if c in 'abcdefghijklmnopqrstuvwxyz': 
       ans = ans + c 
     return ans 

    def isPal(s): 
     if len(s) <= 1: 
      return True 
     else: 
      return s[0] == s[-1] and isPal(s[1:-1]) 

    return isPal(toChars(s)) 

我想实现这样的事情:

s=str(raw_input('Enter a word with quotes: ')) 

我想被要求输入一个字,因为现在,对我的代码运行的唯一方法是调用它的壳。

对话:对不起,我的英语。

+0

'toChars'先后为总理候选人一个列表理解:'返回[c在s中如果c在'abcdefghijklmnopqrstuvwxyz'中]'。另外,您可能需要'string.lowercase'(可能包含á,具体取决于语言环境)。 – Eric

+2

请不要编辑你的问题,要求完全不同的东西。我刚回到下面的答案实际上是回应的问题。 – Blckknght

回答

2

下面将做(不包括引号 - 我不知道为什么你要他们):

s = raw_input('Enter a word: ') 
print isPalindrome(s) 
+0

谢谢!但实际上,print语句必须是:print isPalindrome(s) –

+0

@david_doji:你说得对。纠正。 – NPE

0

这将做

>>> is_a_pal = raw_input('Enter a word with quotes: ') 
Enter a word with quotes: tyuiyt 
>>> is_a_pal 
'tyuiyt' 
>>>