代码之家  ›  专栏  ›  技术社区  ›  Senku's Wife

正在尝试解决“'TypeError:不支持+:'NoneType'和'int'”的操作数类型[closed]

  •  0
  • Senku's Wife  · 技术社区  · 2 年前
    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'
    
    1 回复  |  直到 2 年前
        1
  •  1
  •   Nick Bailey    2 年前

    你不会回来了 numOfQuestions 在if-else的第一个分支中 checkNumOfQuestions .在Python中,如果函数没有显式返回值,它将返回 None .所以当你到达 range(1, numOfQuestions +1) , 裸体问题 没有一个 .在python中,尝试添加两个不兼容的类型通常会给您带来 TypeError ,指定这两个操作数的类型,正如您在这里看到的。