简体   繁体   English

为什么一个额外的无类型 str 正在返回?

[英]Why an extra none type str is returning?

Here is my code in Python for returning a string in capitalized:这是我在 Python 中返回大写字符串的代码:

import math
import os
import random
import re
import sys


def solve(s):
    name = list(s.split())

for i in range(len(name)):
    nparts = name[i].capitalize()
    return print (nparts, end = " ")

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    s = input()

    result = solve(s)

    fptr.write(result + '\n')

    fptr.close()

When I run only the function then the result is ok, but when I try to write the result in a file then I get the error below:当我只运行 function 时,结果还可以,但是当我尝试将结果写入文件时,出现以下错误:

fptr.write(result + '\n')

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

By manually checking I found that when I am storing the result into the result variable it also gets an extra value "None".通过手动检查,我发现当我将结果存储到结果变量中时,它还会获得一个额外的值“无”。 I have no idea why this is going on.我不知道为什么会这样。 Please help.请帮忙。

Your def solve(s): function doesn't return anything so by default it returns None您的def solve(s): function 不返回任何内容,因此默认情况下它返回None

Fix it to:将其修复为:

def solve(s):
    name = list(s.split())

    return name

To capitalize each word a sentence:要将每个单词大写一个句子:

  • split it split
  • loop on it to capitalize each word循环它以capitalize每个单词
  • join them by space通过空间join他们
import os

def solve(sentence):
    return " ".join(word.capitalize() for word in sentence.split())

if __name__ == '__main__':
    s = input("Give a sentence: ")
    result = solve(s)
    with open(os.environ['OUTPUT_PATH'], 'w') as fptr:
        fptr.write(result + '\n')

When the programmer does not define functions to return anything, Python function by default return None .当程序员没有定义函数返回任何东西时, Python function 默认返回None This is the thing that is happening in your program.这就是您的程序中正在发生的事情。

The function solve does not return anything and so returns None which gets stored in the variable result when the function is called function solve不返回任何内容,因此在调用 function 时返回存储在变量result中的 None

A change that you can make in your program is to return the name.您可以在程序中进行的更改是返回名称。

def solve(s):
    
    name = list(s.split())
    return name

Also, in your program, a return statement cannot be used within a for block.此外,在您的程序中,不能在for块中使用return语句。 Moreover, name is not defined in your main program.此外, name未在您的main中定义。 A tip to fix it would be to change the variable name from name to result in your for loop and place the for block after calling the function:修复它的提示是将变量名称从name更改为result您的 for 循环并在调用 function 后放置 for 块:

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    s = input()

    name = solve(s)
    for i in range(len(name)):
        nparts = name[i].capitalize()
        print (nparts, end = " ")

    fptr.write(name[0] + '\n')

    fptr.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM