修改Python类定义使其不区分大小写

2024-10-01 11:39:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个问题,关于修改一个类,使它不区分大小写,并忽略空格。你知道吗

因为这是我的家庭作业,所以我不寻求答案,只寻求一些帮助。你知道吗

赋值要求我修改checkAnswer,因此它不区分大小写。你知道吗

作业的提示是使用字符串replaceupperlower。你知道吗

the homework problem itself

我一直在想:

  def checkAnswer(self, response) :
      responseStr=str(response)
      responseStr.lower()
      responseStr.replace(" ","")
      answer=str(self)
      answer.lower()
      answer.replace(" ","")
  if answer == responseStr:
      return self._answer = correctResponse

但是我不知道如何将reponseStr与另一个文件中的正确答案进行比较。你知道吗

如果有必要,我可以把测试文件包括进来。你知道吗


from random import randint

## A question with a text and an answer.; ignores case and spaces.#
class Question :
## Constructs a question with empty question and answer strings.
#
def __init__(self) :
   self._text = ""
   self._answer = ""

##  Sets the question text.
#   @param questionText the text of this question
#
def setText(self, questionText) :   
   self._text = questionText

## Sets the answer for this question.#
   @param correctResponse the answer

def setAnswer(self, correctResponse) :
   self._answer = correctResponse

## Checks a given response for correctness.
#  @param response the response to check
#  @return True if the response was correct, False otherwise
#
def checkAnswer(self, response) :
   return response == self._answer

## Displays this question.#
def display(self) :
   print(self._text)         

## A question with numeric answer.#
class NumericQuestion(Question) :
    pass
## A question with multiple choices presented in random order.#
class ChoiceQuestion(Question) :
    pass

这是我应该从中读取输入并与正确答案进行比较的文件。
## #HW10.py保存的类定义为:Question、NumericQuestion #选择题

from HW10 import Question, NumericQuestion, ChoiceQuestion

def main() :     
    stem = "What is the value of 10/3 ?"
    nq = NumericQuestion()       
    nq.setText(stem)
    nq.display()
    nq.setAnswer(10/3)
    for i in range(5):
        response = input("Enter answer to two decimal places: ")
        print(response)
        if nq.checkAnswer(response):
            print("Correct!")
        else:
            print("Incorrect")

   print('\n')  
   q = Question()
   q.setText("Who is the inventor of Python?")
   q.setAnswer("Guido van Rossum")
   q.display()
   response = input("Enter answer: ")
   if q.checkAnswer(response):
      print("Correct!")
   else:
      print("Incorrect")

   print('\n')

   mcq = ChoiceQuestion()
   mcq.setText("In which country was the inventor of Python born?")
   mcq.addChoice("Australia", False)
   mcq.addChoice("Canada", False)
   mcq.addChoice("Netherlands", True)
   mcq.addChoice("United States", False)
   for i in range(3) :
       presentQuestion(mcq)

 ## Presents a question to the user and checks the response.
 #  @param q the question
 #
def presentQuestion(q) :
q.display()   # Uses dynamic method lookup.
response = input("Your answer: ")
if q.checkAnswer(response):
   print("Correct")
else:
   print("Incorrect")   # checkAnswer uses dynamic method lookup.

# Start the program.
main()

Tags: thetextanswerselfifresponsedefquestion