Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.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_Camelcasing - Fatal编程技术网

为什么我的标签python脚本转换器不工作?

为什么我的标签python脚本转换器不工作?,python,camelcasing,Python,Camelcasing,我正在编写这个脚本,它将获取一个字符串并将其转换为一个驼峰大小写的hashtag。我遇到的问题是,每次我运行我的代码时,它都会无限期地运行,我不完全确定为什么 if s == "": # a constraint given by the problem return False elif len(s) > 140: # a constraint given by the problem return False else: i = 0 while i &

我正在编写这个脚本,它将获取一个字符串并将其转换为一个驼峰大小写的hashtag。我遇到的问题是,每次我运行我的代码时,它都会无限期地运行,我不完全确定为什么

if s == "": # a constraint given by the problem
    return False
elif len(s) > 140: # a constraint given by the problem 
    return False
else:
    i = 0
    while i < len(s)+1:
        if s[i] == " ": # if there is a space the next character would be a letter, logically
            if s[i+1] != " ": # if the next character is a letter (not a space) it will capitalize it 
                s[i+1].upper()
    i += 1
    return "#" + s.replace(" ", "")
如果s==”:#问题给出的约束
返回错误
elif len>140:#问题给出的约束
返回错误
其他:
i=0
而i
以下是我为您想到的:

def cam(s):

    if s and len(s) <= 140:
        s = s.title().replace(s[0],s[0].lower()).replace(' ','',1)
        return "#"+s
    return False

print(cam('Camel case words are fun.'))

以下是我给你的建议:

def cam(s):

    if s and len(s) <= 140:
        s = s.title().replace(s[0],s[0].lower()).replace(' ','',1)
        return "#"+s
    return False

print(cam('Camel case words are fun.'))

因为
i+=1
被放置在循环之外,所以运行到一个无限循环中

因为
i+=1
被放置在循环之外,所以运行到无限循环中

如果s==”:#问题给出的约束
if s == "": # a constraint given by the problem
    return False
elif len(s) > 140: # a constraint given by the problem 
    return False
else:
    i = 0
    while i < len(s)+1:
        if s[i] == " ": # if there is a space the next character would be a letter, logically
            if s[i+1] != " ": # if the next character is a letter (not a space) it will capitalize it 
                s[i+1].upper()
        i += 1
    return "#" + s.replace(" ", "")
返回错误 elif len>140:#问题给出的约束 返回错误 其他: i=0 而i
如果s==”:#问题给出的约束
返回错误
elif len>140:#问题给出的约束
返回错误
其他:
i=0
而i
即使像一些答案所建议的那样正确缩进
i+=1
,这也是行不通的。你认为这有什么作用:

s[i+1].upper()
Python中的字符串是不可变的——您不能更改它们。这将以大写字母形式返回
s[i+1]
,但不会更改
s
。也就是说,这是一个禁止操作

让我们看看我们是否可以使您的代码在最少的更改下工作:

def hash_camel(s):
    if s == "": # a constraint given by the problem
        return False

    if len(s) > 140: # a constraint given by the problem
        return False

    string = "#"
    i = 0

    while i < len(s):
        if s[i] == ' ':  # if there is a space, the next character could be a letter
            if s[i + 1].isalpha():  # if the next character is a letter, capitalize it
                string += s[i + 1].upper()

                i += 1  # don't process s[i + 1] again in the next iteration

        else:
            string += s[i]

        i += 1

    return string

if __name__ == "__main__":

    strings = [
        '',
        'goat',
        'june bug',
        'larry curly moe',
        'never   odd   or     even',

    ]

    for string in strings:
        print(hash_camel(string))
如果我是从头开始写的,我可能会这样写:

def hash_camel(s):
    if s == "" or len(s) > 140:  # constraints given by the problem
        return None  # it's data, not a predicate, return None

    words = s.split()

    return '#' + words[0] + ''.join(map(str.capitalize, words[1:]))

即使像一些答案所建议的那样正确地缩进
i+=1
,这也是行不通的。你认为这有什么作用:

s[i+1].upper()
Python中的字符串是不可变的——您不能更改它们。这将以大写字母形式返回
s[i+1]
,但不会更改
s
。也就是说,这是一个禁止操作

让我们看看我们是否可以使您的代码在最少的更改下工作:

def hash_camel(s):
    if s == "": # a constraint given by the problem
        return False

    if len(s) > 140: # a constraint given by the problem
        return False

    string = "#"
    i = 0

    while i < len(s):
        if s[i] == ' ':  # if there is a space, the next character could be a letter
            if s[i + 1].isalpha():  # if the next character is a letter, capitalize it
                string += s[i + 1].upper()

                i += 1  # don't process s[i + 1] again in the next iteration

        else:
            string += s[i]

        i += 1

    return string

if __name__ == "__main__":

    strings = [
        '',
        'goat',
        'june bug',
        'larry curly moe',
        'never   odd   or     even',

    ]

    for string in strings:
        print(hash_camel(string))
如果我是从头开始写的,我可能会这样写:

def hash_camel(s):
    if s == "" or len(s) > 140:  # constraints given by the problem
        return None  # it's data, not a predicate, return None

    words = s.split()

    return '#' + words[0] + ''.join(map(str.capitalize, words[1:]))

你能添加预期的输入和输出测试用例吗?你能添加预期的输入和输出测试用例吗。我建议你查一下“驼峰案例”,然后回去重新阅读这个问题。
str.title()!然而,正如您自己的示例所示,这段代码有缺陷。
replace()
调用弊大于利。例如:
“快速与愤怒”
->#快速与愤怒和
“快速与愤怒”
->#快速与愤怒--都不会输出所需的驼峰案例结果
“#快速与愤怒”
谢谢,我很感激:)这不会输出OP描述的内容。我建议你查一下“驼峰案例”,然后回去重新阅读这个问题。
str.title()!然而,正如您自己的示例所示,这段代码有缺陷。
replace()
调用弊大于利。例如:
“快速和愤怒”
->#快速和愤怒和
“快速和愤怒”
->#快速和愤怒--都不会输出所需的驼峰案例结果
“#快速和愤怒”
谢谢,我很感激:)