小编use*_*867的帖子

Python 2.7.5 Palindrome代码混淆

所以我在这里四处寻找,我找到了工作回文的代码

def isPalindrome():
    string = input('Enter a string: ')
    string1 = string[::-1]
    if string[0] == string[(len(string)-1)] and string[1:(len(string)-2)] == string1[1:(len(string)-2)]:
        print('It is a palindrome')
    else:
        print('It is not a palindrome')

isPalindrome()
Run Code Online (Sandbox Code Playgroud)

所以我已经将输入更改为raw_input.它有效.

但在评论中有一个更简单的代码:

def isPalindrome():
    string1 = input('Enter a string: ')
    string2 = string[::-1]
    if string1 == string2:
        return 'It is a palindrome'
    return 'It is not a palindrome'

isPalindrome()
Run Code Online (Sandbox Code Playgroud)

我收到了回读:

Traceback (most recent call last):
  File "C:\Python27\idk1.py", line 8, in <module>
    isPalindrome()
  File "C:\Python27\idk1.py", line 2, in isPalindrome
    string1 …
Run Code Online (Sandbox Code Playgroud)

python python-2.7

0
推荐指数
1
解决办法
413
查看次数

标签 统计

python ×1

python-2.7 ×1