python - Not checking previous parameter -


i'm trying starting out python , i'm trying write basic program checks if word palindrome. there while loop within while loop program isn't backing out check if first loop still applicable.

while n <= half_word_length:     while word[n] == word[word_length - 1 - n]:         n += 1     if word[n] != word[word_length - n]:         print('this word not palindrome.')     break else:     print('this word palindrome.') 

regardless of if input word palindrome or not error message "list index out of range". thought because wasn't checking outside of loop non-palindrome shouldn't cause program crash.

apologies if hard understand, tell me , i'll try fix bit :)

there absolutely no need inner while loop, break incorrectly indented , compare letters first letter (word[0]) not nth letter:

while n <= half_word_length:     if word[n] != word[word_length - n - 1]:         print('this word not palindrome.')         break     n += 1 else:     print('this word palindrome.') 

as julienc points out, though, there many more pythonic approaches this. should @ least using for loop, rather creating , incrementing own index in while loop.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -