Sep-11-2023, 03:03 AM
(Sep-10-2023, 11:13 PM)Skaperen Wrote:(Sep-07-2023, 01:43 PM)PythonBoy Wrote: I am interested in this method also. Could you please explain why did we write y = x[::-1]?which aspect of this are you asking?
1. why assign an extra variable y?
2. what does x[::-1] mean?
i would have written it with one statement instead of two:
x = '1234321' if x == x[::-1]: print('The number is a palindrome') else: print('Number is not palindrome')is that what you were wondering about?
but this has no loops, as your assignment requires. to do it with at least one loop, i would compare the individual characters, one by one, over both ends of the string, until everything has been compared. if any comparisons are unequal then it is not a palindrome. you need to known the length of the string to do this right. i assume you have learned how to get that. you want to have a loop that has two index variables, one for the front, and the other for the back. you compare the front one character to the back one character in the string. i assume know how to do that. if they are unequal, the loop needs to end, right? but if they are equal then it goes on. where does it go and and how does it get there?
I was asking what x[::-1] is. And they helped me figure it out.
Yes that is exactly the way I tried doing it but I got an error. Then I got to know that I use an outdated version of python
When I downloaded the new one, my code worked properly
Thank you!