关于range
s = input("输入:")m = []
n = []
for i in s:
if i.isdecimal():
m.append(i)
else:
n.append(i)
lenm = len(m)
lenn = len(n)
if abs(lenm - lenn) > 1:
print("输出:字符串中数字和字母的数量不满足重新格式化的条件")
else:
if lenm > lenn:
shorter = lenn
longer = lenm
else:
shorter = lenm
longer = lenn
result = []
for each in range(len(shorter)):
result.append(longer)
result.append(shorter)
if len(longer) > len(shorter):
result.append(longer[-1])
print("".join(result))
这串代码哪里出问题了啊?
总是报错:如下所示
应该怎么修改啊!!!
object of type 'int' has no len() 把第 27 行:
for each in range(len(shorter)):
改成:
for each in range(len(str(shorter))): python爱好者. 发表于 2022-2-10 11:30
把第 27 行:
改成:
这里将 shoter 变量变成了字符串,因为想要求一个对象的长度,就必须是字符串类型! s = input("输入:")
m = []
n = []
for i in s:
if i.isdecimal():
m.append(i)
else:
n.append(i)
lenm = len(m)
lenn = len(n)
if abs(lenm - lenn) > 1:
print("输出:字符串中数字和字母的数量不满足重新格式化的条件")
else:
if lenm > lenn:
shorter = n# 改了这里
longer = m# 改了这里
else:
shorter = m# 改了这里
longer = n# 改了这里
result = []
for each in range(len(shorter)):
result.append(longer)
result.append(shorter)
if len(longer) > len(shorter):
result.append(longer[-1])
print("".join(result))
页:
[1]