File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python
2+ # -*- coding: utf-8 -*-
3+
4+ '''
5+ Ask the user for a string and print out whether this string is a palindrome or not.
6+ (A palindrome is a string that reads the same forwards and backwards.)
7+ '''
8+
9+ word = raw_input ('Please input a string for check: ' )
10+
11+ # Method 1:
12+ reverse_word = word [::- 1 ]
13+
14+ if word == reverse_word :
15+ print 'The word you input is a palindrome'
16+ else :
17+ print 'The word you input is not a palindrome'
18+
19+ # Method 2:
20+ is_palindrome = True
21+ length = len (word )
22+ for i in xrange (0 , length / 2 ):
23+ if word [i ] != word [length - 1 - i ]:
24+ is_palindrome = False
25+ break
26+
27+ if is_palindrome :
28+ print 'The word you input is a palindrome'
29+ else :
30+ print 'The word you input is not a palindrome'
You can’t perform that action at this time.
0 commit comments