Question: Python beginner palindrome problem using string slicing https://media.discordapp.net/attachments/518651459429204000/553422729236185088/20190307_214351.jpg?width=504&height=676 The beginning code I have so far is: miles = 100000 # initializes sentry variable while miles
Python beginner palindrome problem using string slicing
https://media.discordapp.net/attachments/518651459429204000/553422729236185088/20190307_214351.jpg?width=504&height=676
The beginning code I have so far is:
miles = 100000 # initializes sentry variable
while miles < 1000000: # runs the loop if miles is under 1 million if str(miles)[2::] == str(miles)[2::-1]: # converts miles to a string and sees if the last 4 digits are equal to the last 4 digits of reverse miles print(miles)
if str(miles + 1)[1::] == str(miles + 1)[1::-1]: # converts miles to a string and sees if the last 5 digits are equal to the last 5 digits of reverse miles if str(miles + 2)[1:5:] == str(miles + 2)[1:5:-1]: # converts miles to a string and sees if the middle 4 digits are equal to the middle 4 digits of reverse miles if str(miles + 3) == str(miles + 3)[::-1]: # converts miles to a string and sees if all 6 digits are equal to the all 6 digits of reverse miles print("The original number on the odometer is", miles) miles += 1 # updates sentry variable
The print after the first if statement was to test if any numbers were completing that condition, but nothing was printing. We have not learned string slicing in my class yet so I may be doing this all wrong. Any help with solving this problem would be appreciated.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
