找出输入字符串的密码
str1 = input('输入你要找的字符串:')length = len(str1)
for i in range(length):
if str1.islower():
if str1.isupper() and str1.isupper() and str1.isupper() and \
str1.isupper() and str1.isupper() and str1.isupper():
if str1.isupper() == False and str1.isupper() == False:
return str1
我这里想实现的是找到这个小写字母,且小写字母前后仅有3个大写字母然后输出这个小写字母,我感觉我的语句没问题哇,但是报错了能给看看嘛 1.return 要写在函数里面
return str1
2.你这样很容易越界报错 确实,这样写很容易下标越界,
建议首尾各加4位小写字母,i 直接从第4位开始到倒数第四位,
这样能很简单的避免下标越界的情况,并且不用加大量的 if 去判断,也不用单独处理越界的情况
inpurt 不支持输入多行字符,你拷贝到 input 实际上 只能接受一行字符
另外防止超出范围 length 应该减 4,参考代码:
str1 = ''' 拷贝过来的字符串 '''
length = len(str1)
for i in range(length-4):
if str1.islower():
if str1.isupper() and str1.isupper() and str1.isupper() and \
str1.isupper() and str1.isupper() and str1.isupper():
if str1.isupper() == False and str1.isupper() == False:
print(str1,end='') range(3, length-3) # 區間前後內缩3位,防止越界 Twilight6 发表于 2021-9-16 12:20
inpurt 不支持输入多行字符,你拷贝到 input 实际上 只能接受一行字符
另外防止超出范围 length 应该 ...
str1 = input('输入你要找的字符串:')
length = len(str1)
for i in range(length-4):
if str1.islower():
if str1.isupper() and str1.isupper() and str1.isupper() and \
str1.isupper() and str1.isupper() and str1.isupper():
if str1.isupper() == False and str1.isupper() == False:
print(str,end = '')
我本意是想用简单的来尝试一下 ,前面的会换的
我改成了这样,为啥还是会报错,Traceback (most recent call last):
File "G:\phthon\练习\p20-2.py", line 9, in <module>
print(str,end = '')
TypeError: 'type' object is not subscriptable乐意看下原因嘛 wodeai999 发表于 2021-9-16 15:34
str1 = input('输入你要找的字符串:')
length = len(str1)
发代码时候发在 代码输入框里,不然你的代码会被编辑器吃了,重新发下吧
编辑器的 “添加代码文字”(这个符号:<>)
Twilight6 发表于 2021-9-16 15:44
发代码时候发在 代码输入框里,不然你的代码会被编辑器吃了,重新发下吧
编辑器的 “添加代码文字 ...
已解决谢谢哇
页:
[1]