2014-11-07 60 views
0

我是python新手,不太了解,但认为我可以很好地进行测验,并希望他们有点复杂。如何用多个主题在python中创建一个简单的测验?

我该怎么做才能使程序首先让用户选择测试类型。 (即动物或首都城市),然后提交给用户的问题将与该主题有关。我应该为每个主题创建一组功能吗?然后,当用户输入他们想要的主题,代码应该是这个样子:(大致)

print("Do you want to answer questions on animals or capital cities or math?" 
     " Type animal, city or math") 
topic = input() 
if topic == 'animal': 
    def AnimalQuestions(): #this will be written before this code 

这是正确的做法还是有另一种更有效的方式来做到这一点?

+3

的所有代码首先是(无犯罪)都乱作一团,一个你完全无视':'S,不要使用普通的'input'除非必要或者您在Python 3。此外,请解释你将如何进行测验,字符串格式,问题和答案列表,字典,服务器等。你没有真正提供足够的信息来回答你的问题。也欢迎堆栈溢出! – tox123 2014-11-07 01:53:36

+0

@Carar你能否在 – Montaro 2014-11-07 12:14:21

回答

2

我实现了真与假测验,每个主题中多个问题,加上输入和结果的聚集验证一个完整的例子,我希望这可以是一个很好的例子

animals_questions = 'Animals Questions' 
capitals_questions = 'Capitals Questions' 
math_questions = 'Math Questions' 

questions = [animals_questions, capitals_questions, math_questions] 

quiz = {animals_questions: [("All lionesses in a pride", True), 
         ("Another animals question", False), 
         ("Last animals question", False)], 

     capitals_questions: [("Cairo is the capital city of Egypt", True), 
         ("Another capitals question", True), 
         ("Last capitals question", False)], 

     math_questions: [("20 is log 100 for base 1o", False), 
        ("Another math question", True), 
        ("Last math question", False)] 
} 

result = {"Correct": 0, "Incorrect": 0} 

def get_quiz_choice(): 
    while True: 
     try: 
      quiz_number = int(raw_input(
       'Choose the quiz you like\n1 for {}\n2 for {}\n3 for {}\nYour choice:'.format(animals_questions, 
                          capitals_questions, 
                          math_questions))) 
     except ValueError: 
      print "Not a number, please try again\n" 
     else: 
      if 0 >= quiz_number or quiz_number > len(quiz): 
       print "Invalid value, please try again\n" 
      else: 
       return quiz_number 


def get_answer(question, correct_answer): 
    while True: 
     try: 
      print "Q: {}".format(question) 
      answer = int(raw_input("1 for True\n0 for False\nYour answer: ")) 
     except ValueError: 
      print "Not a number, please try again\n" 
     else: 
      if answer is not 0 and answer is not 1: 
       print "Invalid value, please try again\n" 
      elif bool(answer) is correct_answer: 
       result["Correct"] += 1 
       return True 
      else: 
       result["Incorrect"] += 1 
       return False 


choice = get_quiz_choice() 
quiz_name = questions[choice - 1] 
print "\nYou chose the {}\n".format(quiz_name) 
quiz_questions = quiz[quiz_name] 
for q in (quiz_questions): 
    print "Your answer is: {}\n".format(str(get_answer(q[0], q[1]))) 

的输出是一样的东西:

/usr/bin/python /Users/arefaey/workspace/playground/python/Quiz/quiz.py 

Choose the quiz you like 
1 for Animals Questions 
2 for Capitals Questions 
3 for Math Questions 
Your choice: 2 

You chose the Capitals Questions 

Q: Cairo is the capital city of Egypt 
1 for True 
0 for False 
Your answer: 1 
Your answer is: True 

Q: Another capitals question 
1 for True 
0 for False 
Your answer: 1 
Your answer is: True 

Q: Last capitals question 
1 for True 
0 for False 
Your answer: 1 
Your answer is: False 

You have finished the quiz with score: {'Incorrect': 1, 'Correct': 2} 
1

这应该让你在正确的轨道上,但是您的代码示例判断它可能支付必须通过Python文档读取和/或一展身手,在一些教程材料@http://www.learnpython.org/

def animal_quiz(): 
    # do something 

def city_quiz(): 
    # do something 

def math_quiz(): 
    # do something 

topic = raw_input('Do you want to answer questions on animals or capital cities or math? Type animal, city or math') 
if topic == 'animal': 
    animal_quiz() 
elif topic == 'city': 
    city_quiz() 
elif topic == 'math': 
    math_quiz() 
+0

以下检查我的答案所以在defs中,我会把完整的测验,就好像我只是在做那个话题?这是最简单的事情吗? – Carar 2014-11-07 04:06:33

+0

是的,将代码分解成单独的方法来执行单独的作业,使代码更易于阅读。 – Karl 2014-11-09 18:31:42

相关问题