TypeError:只能将str(而不是“ NoneType”)连接到str

时间:2019-04-01 07:27:04

标签: python string typeerror

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

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)

这是我的输出:

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

我真的是Python的新手,我看到了'list to list'TypeErrors和one other answer的str到str的答案,但是我无法将其与我的代码关联。如果有人可以解释我在做什么错,那太好了!

3 个答案:

答案 0 :(得分:0)

正如Coldspeed所说,当您将小写字母传递给convertChar时,该函数将不会返回正确的字符。因此是错误。

此外,使用“字符串=字符串+ convertChar(char)”,您将附加到相同的输入字符串。错了您需要为此使用一个新的空字符串。

答案 1 :(得分:0)

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

您可以执行以下操作:

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

    return char

这可能不是最理想的解决方案,但是它将解决大多数问题。即,仅当输入的字符为大写字母时,才将其转换为小写字母。对于所有其他情况(无论是小写字符,数字等),都将按原样返回该字符。

第二,如果要使用大写到小写的转换器,则HElLo的输出应为hello,而不是HElLohello

为此,您需要修改第二个功能,如下所示:

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

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

用法示例:

  

'Hello'.upper()

答案 2 :(得分:0)

我不知道构建您自己的方法的原因,但您可以改用内置的 lower() 方法。例如:

<entry>
    <record>583</record>
    <time>2020/12/20 13:48:37.553</time>
    <type>Information</type>
    <source>VisualStudio</source>
    <description>Begin package load [ErrorListPackage]</description>
    <guid>{4A9B7E50-AA16-11D0-A8C5-00A0C921A4D2}</guid>
  </entry>
  <entry>
    <record>584</record>
    <time>2020/12/20 13:48:38.803</time>
    <type>Error</type>
    <source>VisualStudio</source>
<description>CreateInstance failed for package [ErrorListPackage]Source: &apos;mscorlib&apos; Description: Could not load file or assembly &apos;Microsoft.VisualStudio.ErrorListPkg, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a&apos; or one of its dependencies. The module was expected to contain an assembly manifest.&#x000D;&#x000A;System.BadImageFormatException: Could not load file or assembly &apos;Microsoft.VisualStudio.ErrorListPkg, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a&apos; or one of its dependencies. The module was expected to contain an assembly manifest.&#x000D;&#x000A;File name: &apos;Microsoft.VisualStudio.ErrorListPkg, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a&apos;&#x000D;&#x000A;   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark&amp; stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)&#x000D;&#x000A;   at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark&amp; stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)&#x000D;&#x000A;   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark&amp; stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)&#x000D;&#x000A;   at System.Activator.CreateInstance(String assemblyString, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo, StackCrawlMark&amp; stackMark)&#x000D;&#x000A;   at System.Activator.CreateInstance(String assemblyName, String typeName)&#x000D;&#x000A;   at System.AppDomain.CreateInstance(String assemblyName, String typeName)&#x000D;&#x000A;&#x000D;&#x000A;WRN: Assembly binding logging is turned OFF.&#x000D;&#x000A;To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.&#x000D;&#x000A;Note: There is some performance penalty associated with assembly bind failure logging.&#x000D;&#x000A;To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].&#x000D;&#x000A;</description>
    <guid>{4A9B7E50-AA16-11D0-A8C5-00A0C921A4D2}</guid>
    <hr>80004005 - E_FAIL</hr>
    <errorinfo></errorinfo>