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

时间:2017-04-19 14:37:41

标签: 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     #ChoiceQuestion

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()

0 个答案:

没有答案