循环检查我的python测验中的答案

时间:2017-11-21 13:51:04

标签: python loops

下面是我创建多级测验的python代码。我几乎拥有一切,但是让我的代码检查答案,并且我的代码打印问题是我的想法。我对此也很陌生,所以请随时查看此代码并帮助您查看任何可能的冗余。

from datetime import datetime
now = datetime.now()

# showing the current date you're taking the quiz
print("Today's Date is:") 
print('%s/%s/%s' % (now.month, now.day, now.year))

print("")

#this will print out my welcome statement and ask you for your name
print('Hello welcome to my Quiz, what is your name?')


#definied the variable username so that the user could type in their name
usersname = input()
print ("")
print('It is nice to meet you, ' + usersname)




def level_selection():
  select_level = input ("easy | medium | hard")
  if select_level == "easy":
    print (usersname + "+" "you have selected the easy route good luck!")
    return easy_questions
  if select_level == "medium":
    print (usersname + "+" "you have selected the medium route good luck!")
    return medium_questions
  if select_level == "hard":
    print (usersname + "+" "you have selected the easy route good luck!")
    return hard_questions
  else:
    print("Do you not follow instructions lol please try again and select easy medium or hard")
    return level_selection




#these are my questions & answers

easy_questions= ["What is 20 + 2 __1__", "What is the capital of Georgia__2__","If john has $4 and buys a candy bar for $3.50, how much money does he have left over__3__","What is the name of the NBA basketball team in Georgia__4__","What is the name of the NFL team in Georgia__5__","How many toys do I have if I sell 3, but had 8 toys originally__6__","How many Bad Boy movies are there__7__","How many Fast Furious movies are there__8__","What is 10 * 5 __9__","What city does the UGA team play in__10__"]

answers_list_easy = ["Cleveland", "Atlanta", "Michael Jordan", "Oklahoma City Thunder","Five","Falcons","Georgia","California","UCLA","Detroit"]

medium_questions= ["What is 20 + 2 __1__", "What is the capital of Georgia__2__","If john has $4 and buys a candy bar for $3.50, how much money does he have left over__3__","What is the name of the NBA basketball team in Georgia__4__","What is the name of the NFL team in Georgia__5__","How many toys do I have if I sell 3, but had 8 toys originally__6__","How many Bad Boy movies are there__7__","How many Fast Furious movies are there__8__","What is 10 * 5 __9__","What city does the UGA team play in__10__"]

answers_list_medium = ["two", "Adam Sandler", "Michael Jordan", "Patriots","UGA","Four","Atlanta","Augusta","Athens","2011"]

hard_questions= ["What is 20 + 2 __1__", "What is the capital of Georgia__2__","If john has $4 and buys a candy bar for $3.50, how much money does he have left over__3__","What is the name of the NBA basketball team in Georgia__4__","What is the name of the NFL team in Georgia__5__","How many toys do I have if I sell 3, but had 8 toys originally__6__","How many Bad Boy movies are there__7__","How many Fast Furious movies are there__8__","What is 10 * 5 __9__","What city does the UGA team play in__10__"]

answers_list_hard = ["5460", "1", "Two", "No","Five","Michael Jordan","Malcom Borgdon","Migos","Spurs","Atlanta Falcons"]

#This is using {} "curly brackets"
game_questions_answers = {
   'easy': {
        'quiz': easy_questions,
        'answers': answers_list_easy,
        'message': "You chose easy, and if you can't pass this test then your lost LOL"
    },
   'medium': {
        'quiz': medium_questions,
        'answers': answers_list_medium,
        'message': "You chose medium, game on hope your ready HAHAHA!!!!"
    },
   'hard': {
        'quiz': hard_questions,
        'answers': answers_list_hard,
        'message': "May the force be with you *evil laugh*!!"
    }
}

1 个答案:

答案 0 :(得分:1)

如果您希望代码检查并打印问题,那么您需要定义一些函数,一个用于打印问题,另一个用于检查用户获得的答案数量。

一旦您发现问题基本上是字符串列表,打印问题就很容易了。您只需要一个函数,它将list作为参数并打印其所有值(使用循环或递归):

def print_questions(question_list):
    len = len(question_list)
    for i in range(0, len):
        print(question_list[i])

但是,如果您希望用户在每个问题(我怀疑您这样做)之后输入答案,那么您就无法使用上述功能。相反,请考虑以下一点:

def check(question_lst, answer_lst):
    user_answers = []                        # empty list to keep track of user's answers
    length = len(question_lst)
    for i in range(0, length):
        user_answer = input(question_lst[i])    # record user input
        if user_answer == answer_lst[i]     # check if user input equals the corresponding entry in the answer list
            user_answers.append(1)           # if the user's answer is correct, then add a 1 to the list
        user_answers.append(0)               # otherwise, add a 0
    return user_answers

请注意,上面的函数返回一个包含0或1的新列表。然后,您可以定义另一个计算此列表中1的数量的函数,该函数对应于用户正确的问题数量:

score_array = check(easy_questions, answer_list_easy)              # using the "easy" questions

def final_score(score_lst):
    score = 0
    length = len(score_lst)
    for i in range(0, length):
       if score_lst[i] == 1:
           score = score + 1
     return score