Trying to solve a 'TypeError: unsupported operand type(s) for +: 'NoneType' and 'int''

Trying to solve a 'TypeError: unsupported operand type(s) for +: 'NoneType' and 'int''

import sys

def displayGreeting():
     print("\nThe Wizard will see you now."
                                      + "\n"
                                      + "\nOK, let's get started\n")


def askQuestions():
    counter = 0
    
    name = input("What's your name? ")
    
    age = checkAge()
    
    numOfQuestions = checkNumofQuestions()
    

    for counter in range(1, numOfQuestions + 1):
         
         question = input("\nWhat's your question?")

         if question.strip() == "":
              print("You should enter a question")
              continue


         if question.strip().lower() == 'bye':
              break
         else:
              message = processQuestion(counter, question)
              
              print(message)
              
              print("\nNo more questions for you!")
              
              print("Stop bothering the Wizard!")
          
                
def checkNumofQuestions():
    numOfQuestions = input("\n\nHow many Questions do you want to ask the wizard? ")
    
    if numOfQuestions.isnumeric():
         numOfQuestions = int(str(numOfQuestions))
         
         numOfQuestions += 1
         
         print("I am giving you a bonus. You can ask", numOfQuestions, "questions!")
    else:
         print("Warning: ", numofQuestions, "is not a Valid number! I am changing it to 5")

         numOfQuestions = 5

         return numOfQuestions

def checkAge():
    while True:
        age = input("What is your age? ")

        if age.isnumeric():
             
            age = int(age)
            
            if age < 10 or age > 80:
                 print("please enter a valid age between 10 and 80")
            elif age > 30 and age < 50:
                 print("these are prime earning years")
                 break
            else:
                 print("you are a good age to play this game")
                 break
        else:
            print("Please enter a valid integer for your age")
            return age
            
def processQuestion(counter, question):

    if question.startswith("Who"):
        message = "Who, Who... isn't that a sound an owl makes?"
    elif question.startswith("What"):
        message = "What is the meaning of life?"
    elif question.startswith('How'):
            message = "How now, brown cow?" 
    else:
        message = "I don't know"
        message = str(counter) + ". The Wizard answers: " + message + "?"
        return message
       
def main():
     print("Assignment 4\n")

     print("* * The Wizard Game * *")

     playGame = input("Do you want to talk to the Wizard? (Yes or No) ")

     playGame = playGame.strip()

     if playGame.lower() == "yes":
          displayGreeting()
          
          askQuestions()
     else:
        print("The Wizard wants you to go away now!")
        print("\n\nEND OF PROGRAM")


main()

我什至不知道从哪里开始。我尝试了一些东西,但它根本没有帮助。我仍然遇到错误。

这是完整的回溯错误:

    Traceback (most recent call last):
  File "filename.py", line 107, in <module>
    main()
  File "filename.py", line 101, in main
    askQuestions()
  File "filename.py", line 19, in askQuestions
    for counter in range(1, numOfQuestions + 1):
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

您没有在 checkNumOfQuestions 中 if-else 的第一个分支中 returning numOfQuestions。在 Python 中,如果一个函数没有明确地 return 一个值,它 returns None。所以当你到达 range(1, numOfQuestions +1) 时,numOfQuestions 就是 None。在 python 中,尝试添加两个不兼容的类型通常会得到一个 TypeError,指定两个操作数的类型,如您所见。