求助课后19题判断是否为回文联
我的代码:>>>def huiwen(Str):
length = len(Str)
flag = 0
for each in Str:
if Str == Str:
flag=1
else:
print('No')
if flag == 1:
print('Yes')
>>>Str = input()
23454321
>>> huiwen(Str)
报错信息:
Traceback (most recent call last):
File "<pyshell#118>", line 1, in <module>
huiwen(Str)
File "<pyshell#112>", line 5, in huiwen
if Str == Str:
TypeError: string indices must be integers
想不懂为什么捏 each是字符串,不能做Str的下标。 冬雪雪冬 发表于 2018-5-11 14:29
each是字符串,不能做Str的下标。
可是在上一课的练习找子字符串个数里却可以这么用?
def findstr(desStr,subStr):
if subStr not in desStr:
print("can't find it")
else:
for each1 in range(length - 1):
if desStr == subStr:
if desStr == subStr:
count += 1
print("the target substring totally appears %d times" % count) 另外请问怎么样才能在idle里打出中文呀,用复制粘贴的方式ok,但是不能直接输入好麻烦 Erisano 发表于 2018-5-11 14:40
可是在上一课的练习找子字符串个数里却可以这么用?
def findstr(desStr,subStr):
if subStr not in d ...
这里each1是整数,你对比一下两个for语句 Erisano 发表于 2018-5-11 14:55
另外请问怎么样才能在idle里打出中文呀,用复制粘贴的方式ok,但是不能直接输入好麻烦
你用的是apple电脑吗?windows系统不存在问题,如果是mac系统搜索一下以前的帖子,有解决方法。 冬雪雪冬 发表于 2018-5-11 15:11
这里each1是整数,你对比一下两个for语句
我换成range就可以了,谢谢!
页:
[1]