类型错误:只能将 str(不是“NoneType")连接到 str [英] TypeError: can only concatenate str (not "NoneType") to str

查看:84
本文介绍了类型错误:只能将 str(不是“NoneType")连接到 str的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在 Python 3.7 中构建大写到小写字符串转换器,这是我的代码:

def convertChar(char):如果 char >='A' 和 char <='Z':返回 chr(ord(char) + 32)def toLowerCase(string):对于字符串中的字符:字符串 = 字符串 + convertChar(char)返回字符串string = input("请输入字符串-")结果 = toLowerCase(string)打印(结果)

这是我的输出:

输入字符串 - HELLo回溯(最近一次调用最后一次): 中的文件loweralph.py",第 11 行字符串 = toLowerCase(结果)文件loweralph.py",第 6 行,在 toLowerCase 中结果 = 结果 + convertChar(char)类型错误:只能将 str(不是NoneType")连接到 str

我对 Python 真的很陌生,我看到了从列表到列表"类型错误的答案和 str 到 str 的另一个答案,但我无法将它与我的代码联系起来.如果有人能解释我做错了什么,那就太好了!

解决方案

您需要在代码中添加一些异常情况.首先,如果输入的字符已经是小写怎么办?

你可以做这样的事情:

def convertChar(char):如果 char >='A' 和 char <='Z':返回 chr(ord(char) + 32)返回字符

这可能不是最理想的解决方案,但它可以解决大部分问题.也就是说,只有输入的字符是大写的,才会转换为小写.对于所有其他情况(无论是小写字符、数字等),字符将按原样返回.

其次,如果您要制作大写到小写的转换器,HElLo 的输出应该是 hello,而不是 HElLohello.

为此,您需要按如下方式修改第二个函数:

def toLowerCase(string):新字符串 = []对于字符串中的字符:newString.append(convertChar(char))返回 '​​'.join(newString)

最后,您可能需要考虑使用 .upper() 内置函数.

用法示例:

<块引用>

'你好'.upper()

Trying to build an uppercase to lowercase string converter in Python 3.7, this is my code:

def convertChar(char):
    if char >= 'A' and char <= 'Z':
        return chr(ord(char) + 32)
def toLowerCase(string):
    for char in string:
        string = string + convertChar(char)
    return string
string = input("Enter string - ")
result = toLowerCase(string)
print(result)

And this is my output:

Enter string - HELlo
Traceback (most recent call last):
  File "loweralph.py", line 11, in <module>
    string = toLowerCase(result)
  File "loweralph.py", line 6, in toLowerCase
    result = result + convertChar(char)
TypeError: can only concatenate str (not "NoneType") to str

I am really new to Python and I saw answers for 'list to list' TypeErrors and one other answer for str to str, but I couldn't relate it to my code. If somebody could explain what I'm doing wrong, it would be great!

解决方案

You need to add some exception cases to your code. Firstly, what if the entered character is already lower case?

You can do something of this sort:

def convertChar(char):
    if char >= 'A' and char <= 'Z':
        return chr(ord(char) + 32)

    return char

This might not be the most ideal solution, but it will take care of most of the things. That is, only if the entered character is upper case, it is converted to lower case. For all other cases (whether it is a lower case character, a number et cetra), the character is returned as it is.

Secondly, if you are making a upper case to lower case converter, output of HElLo should be hello, not HElLohello.

For this, you need to modify your second function as follows:

def toLowerCase(string):
    newString = []
    for char in string:
        newString.append(convertChar(char))
    return ''.join(newString)

Finally, you might want to consider using .upper() in-built function.

Example of usage:

'HeLlo'.upper()

这篇关于类型错误:只能将 str(不是“NoneType")连接到 str的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆