Python 如何修复有关索引字符串的代码?
我做了一个函数来输入字符串并返回带有两个索引的head和tail,没有空格和标点符号。但它只返回“空字符串”Python 如何修复有关索引字符串的代码?,python,Python,我做了一个函数来输入字符串并返回带有两个索引的head和tail,没有空格和标点符号。但它只返回“空字符串” def hello(word): str_cnt=“” 对于大写字母: 如果字母不在string.whitespace中,字母不在string.标点符号中: str_cnt+=字母 如果len(str_cnt)
def hello(word):
str_cnt=“”
对于大写字母:
如果字母不在string.whitespace中,字母不在string.标点符号中:
str_cnt+=字母
如果len(str_cnt)<2:
返回“空字符串”
其他:
返回str_cnt[:2]+str_cnt[-2:]
word=输入(“输入字符串:”)
结果=你好(word)
打印(“结果:”,结果)
我希望输入“hello world!”,实际输出为“Hold”
或者“Hi!”=“HiHi”。如果块:
import string
def hello(word):
str_cnt = ""
for letter in word:
if letter not in string.whitespace and letter not in string.punctuation:
str_cnt += letter
if len(str_cnt) < 2 :
return "empty string"
else:
return str_cnt[:2] + str_cnt[-2:]
word = input("Input String : ")
result = hello(word)
print("Result: ",result)
您的问题是,在工作的第一次迭代之后,无论发生什么,您都会返回
将返回
nogic移动到逻辑之后:
def hello(word):
str_cnt = ""
for letter in word:
if letter not in string.whitespace and letter not in string.punctuation:
str_cnt += letter
if len(str_cnt) < 2 :
return "empty string"
else:
return str_cnt[:2] + str_cnt[-2:]
def hello(word):
str_cnt=“”
对于大写字母:
如果字母不在string.whitespace中,字母不在string.标点符号中:
str_cnt+=字母
如果len(str_cnt)<2:
返回“空字符串”
其他:
返回str_cnt[:2]+str_cnt[-2:]
问题在于,正如大家所说的那样,在纠正了它的工作原理之后,缩进是存在的。我会做得更通俗一些,比如:
def hello(word):
w = ''.join([x for x in word if x not in string.whitespace and x not in string.punctuation])
return w[:2] + w[-2:] if len(w) > 1 else 'empty string'
用法:
>>> hello('hello world!')
held
问题在于缩进不正确:
import string
def hello(word):
str_cnt = ""
for letter in word:
if letter not in string.whitespace and letter not in string.punctuation:
str_cnt += letter
if len(str_cnt) < 2:
return "empty string"
return str_cnt[:2] + str_cnt[-2:]
word = input("Input String: ")
result = hello(word)
print("Result: ", result)
然而,如果输入很长,这是解决问题的错误方法。我们在空白
和标点
列表中测试了许多我们永远不会使用的字符。相反,我们应该从列表的两端抓取前两个有效字符,忽略中间的字符。比如:
def hello(word):
unwanted = string.whitespace + string.punctuation
str_start = ""
for letter in word:
if letter not in unwanted:
str_start += letter
if len(str_start) == 2:
break
if len(str_start) < 2:
return "empty string"
str_end = ""
for idx in range(len(word) - 1, -1, -1):
if word[idx] not in unwanted:
str_end = word[idx] + str_end
if len(str_end) == 2:
break
return str_start + str_end
字母“lecommunicationo”
从未经过测试,因为它们对最终结果没有影响。你想做什么?@thexsecret,我添加了第二个示例,试图避免不必要的测试。
import string
def hello(word):
str_cnt = ""
for letter in word:
if letter not in string.whitespace and letter not in string.punctuation:
str_cnt += letter
if len(str_cnt) < 2:
return "empty string"
return str_cnt[:2] + str_cnt[-2:]
word = input("Input String: ")
result = hello(word)
print("Result: ", result)
> python3 test.py
Input String: hello world!
Result: held
>
def hello(word):
unwanted = string.whitespace + string.punctuation
str_start = ""
for letter in word:
if letter not in unwanted:
str_start += letter
if len(str_start) == 2:
break
if len(str_start) < 2:
return "empty string"
str_end = ""
for idx in range(len(word) - 1, -1, -1):
if word[idx] not in unwanted:
str_end = word[idx] + str_end
if len(str_end) == 2:
break
return str_start + str_end
> python3 test2.py
Input String: telecommunications!
Result: tens
>