Discuss / Python / 啊,好难啊,展现一下我的蠢代码

啊,好难啊,展现一下我的蠢代码

Topic source

爷左_Cola

#1 Created at ... [Delete] [Delete and Lock User]

def trim(s):

s=input('请输入你的字符串')

j=len(s)

i=0

n=s[0:1]

while n==' ':

n=s[i+1:i+2]

i=i+1

m=s[j-2:j-1] 

while m==' ':

m=s[j-3:j-2]

j=j-1

return s

爷左_Cola

#2 Created at ... [Delete] [Delete and Lock User]

第一个回复错的,第二个好一点,但是还是遇到如果最后一个字符不是空的情况有问题,我再修改修改。。。。好烦啊。

def trim(s):
	
	j=len(s)
	i=0

	n=s[0:1]
	while n==' ':
		n=s[i+1:i+2]
		i=i+1

	m=s[j-2:j-1] 
	while m==' ':
		m=s[j-3:j-2]
		j=j-1
	s=s[i:j-1]
	return s


print(trim(' 测 试 文 本 '))

爷左_Cola

#3 Created at ... [Delete] [Delete and Lock User]

第三个版!这个肯定对!。。。。我测了。。。。。

def trim(s):
	
	j=len(s)
	i=0

	n=s[0:1]
	while n==' ':
		n=s[i+1:i+2]
		i=i+1

	m=s[j-1:j] #
	#if m!=' ':

	while m==' ':
		m=s[j-2:j-1]
		j=j-1
	
	s=s[i:j]
	return s


print(trim('  j fsfs  '))

爷左_Cola

#4 Created at ... [Delete] [Delete and Lock User]
def trim(s):
	
	j=len(s)
	i=0

	n=s[0:1]
	while n==' ':
		n=s[i+1:i+2]
		i=i+1
	m=s[j-1:j]
	while m==' ':
		m=s[j-2:j-1]
		j=j-1
	s=s[i:j]
	return s
trim('  j fsfs  ')

  • 1

Reply