python字符串索引超出范围怎么解决_如何修复索引器错误:字符串索引超出范围...

我主要是一个Java用户,但是对于GCSE,我必须使用Python。

我在做一个算法来寻找回文数字。

代码是一个大混乱,因为我。一-还没有完全搞清楚Python语法。还有,2-没有缩短算法,只是让它从更高的数字开始,然后向后工作。

python 3.4.1的代码如下:print("This program will find the nearest palindromic number to a given value.")

countN = input("Input a number above 0 to find the nearest palindromic number")

countN = str(countN)

nl = len(countN) # Number length

pf = 0 # Palindrome found

while(pf == 0 and int(countN)>=0): #While no palindrome found and count is bigger than 0

ep = nl # end pointer

sp = -1 # start pointer

while(int(ep) >= sp):

if(int(countN[ep]) == int(countN[sp])):

ep = ep-1

sp = sp+1

print(count + " is a palindrome.")

pf = 1

else:

print(count + " is not a palindrome.")

countN = countN - 1

我只是想改变指针位,它应该和IF语句分开吗?

无论如何,我遇到的错误如下:>>>

This program will find the nearest palindromic number to a given value.

Input a number above 0 to find the nearest palindroic number99

Traceback (most recent call last):

File "C:/Python34/Computer science/palindrome finder.py", line 10, in

if(int(countN[ep]) == int(countN[sp])):

IndexError: string index out of range

>>>

请注意:我在这里寻找答案,部分原因是str()和int()无处不在。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值