Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何修复有关索引字符串的代码?_Python - Fatal编程技术网

Python 如何修复有关索引字符串的代码?

Python 如何修复有关索引字符串的代码?,python,Python,我做了一个函数来输入字符串并返回带有两个索引的head和tail,没有空格和标点符号。但它只返回“空字符串” def hello(word): str_cnt=“” 对于大写字母: 如果字母不在string.whitespace中,字母不在string.标点符号中: str_cnt+=字母 如果len(str_cnt)

我做了一个函数来输入字符串并返回带有两个索引的head和tail,没有空格和标点符号。但它只返回“空字符串”

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
>