+ 2
Palindrom Words
x = input() print(str(x)) n=len(x) if n%2==0: for i in range(0,len(x)//2): pos =i if x[pos] != x[len(x)-(pos+1)]: print("the word is not palindrom") break print("The word is palindrom") else: for i in range(0,len(x)//2 +1): pos =i if x[pos] != x[len(x)-(pos+1)]: print("the word is not palindrom") break print("The word is palindrom") Where is the error? Thanks very much!
2 odpowiedzi
+ 8
It will always print "The word is palindrom". You can use a variable to avoid that:
palindrome = True
for i in range(0,len(x)//2 +1):
pos =i
if x[pos] != x[len(x)-(pos+1)]:
palindrome = False
break
if palindrome:
print("The word is palindrom")
else:
print('no palindrom')
Also, your break statements aren't indented enough. They will always break the loop in the first iteration
+ 2
Thanks