Here’s a problem that I was trying to solve:
Ask the user for a string and print out whether this string is a palindrome or not. (A palindrome is a string that reads the same forwards and backwards.)
The code that I wrote was
print('Please type in one word which you want to check!')
num=input()
if (int(len(num))%2)==0:
for i in range(int(len(num)/2)):
if num[i]==num[-(i+1)]:
if num[int((len(num)/2)-1)]==num[-(i+1)]:
print('Yes the word is palidrome.')
break
else:
pass
else:
print('No. The word is not palindrome.')
break
elif (int(len(num))%2)!=0:
for i in range(int((len(num)-1)/2)):
if num[i]==num[-(i+1)]:
if num[int(((len(num)-1)/2)-1)]==num[-(i+1)]:
print('Yes the word is palidrome.')
break
else:
pass
else:
print('No. The word is not palindrome.')
break
The code is working perfectly fine.
The problem is that I have to relaunch the Shell every time I want to see whether a word is palindrome or not. So I thought that it would be cool to use a while loop.
I changed the code as follows ( by simply writing while True
and leaving some spaces as shown below ) :
while True:
print('Please type in one word which you want to check!')
num=input()
if (int(len(num))%2)==0:
for i in range(int(len(num)/2)):
if num[i]==num[-(i+1)]:
if num[int((len(num)/2)-1)]==num[-(i+1)]:
print('Yes the word is palidrome.')
break
else:
pass
else:
print('No. The word is not palindrome.')
break
elif (int(len(num))%2)!=0:
for i in range(int((len(num)-1)/2)):
if num[i]==num[-(i+1)]:
if num[int(((len(num)-1)/2)-1)]==num[-(i+1)]:
print('Yes the word is palidrome.')
break
else:
pass
else:
print('No. The word is not palindrome.')
break
Python shows:
Inconsistent use of tabs and spaces in indentation
Why am I getting this and how do I fix it?