Skip to content

Commit 9fdd63c

Browse files
committed
add exercise 6
1 parent 97b0dcf commit 9fdd63c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

exercises_6.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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'

0 commit comments

Comments
 (0)