获取错误“TypeError:'NoneType'对象不可迭代”

问题描述 投票:1回答:1

我该如何解决这个错误?用户测试函数需要保存字符串和字符的输入,然后需要在反向和字符计数函数中调用这些输入。

def user_test():
    '''
    :return: hold user input
    '''
    global user_string
    global user_character
    user_string = print(input("What is the string you would like to use:"))
    user_character = print(input("What is the character you would like to use:"))
    character_count(user_string, user_character)
    reverse(user_string)
    return user_string, user_character;

def reverse(user_string):
    '''
    :param: string
    :return: reverses a string
    '''
    string = user_string
    print (list(reversed(string)))
    return string

def character_count(user_string, user_character):
    '''
    :return: counts the number of occurances of a character in a string
    '''
    string = user_string
    toCount = user_character
    counter = 0

    for letter in string:
            if( letter == toCount):
                    counter += 1
    print(counter)

def main():
    user_test()


main()

追溯

Traceback (most recent call last):
  File "C:\Users\Cheryl\Documents\Python Stuff\lab6_design.py", line 111, in <module>
    main()
  File "C:\Users\Cheryl\Documents\Python Stuff\lab6_design.py", line 108, in main
    user_test()
  File "C:\Users\Cheryl\Documents\Python Stuff\lab6_design.py", line 9, in user_test
    character_count(user_string, user_character)

  File "C:\Users\Cheryl\Documents\Python Stuff\lab6_design.py", line 34, in character_count
for letter in string:
TypeError: 'NoneType' object is not iterable
python-3.x python-3.7
1个回答
1
投票

print()返回None,因此user_string和user_character为None。

user_string = print(input("What is the string you would like to use:"))
user_character = print(input("What is the character you would like to use:"))

如果您仍想打印输入,请将其更改为以下内容

user_string = input("What is the string you would like to use:")
print(user_string)
user_character = input("What is the character you would like to use:")
print(user_character)
© www.soinside.com 2019 - 2024. All rights reserved.