试图在python中创建空白测验的填充

时间:2018-02-18 17:24:37

标签: python-3.x

我必须填写我的在线课程的空白测验,但我似乎陷入困境,而且我很难找到为什么我被卡住了。任何建议都会很棒,谢谢!到目前为止,这是我的代码...当我运行代码时,它会问第一个问题,并且正在寻找答案,但是当我把它放进去时,给我的是#34;不完全,请再试一次#34;提示所以我没有正确地回答这个问题吗?

print ('Welcome to my computer programming Quiz, in this quiz you will be 
asked to select from three difficulty levels, easy, medium, and hard. Each 
the level contains 4 - 5 fill in the blank questions')
print ('')
print ('Your score at the end will be based on how many correct answers you 
get compared to the number of guesses you take')
print ('')
# opening introduction to the Quiz

level=None
while level not in ['easy', 'medium', 'hard']:
    level = input ('Please select a difficulty (easy, medium, or hard):').lower() 
    # This is where the difficulty is chosen, this is also is the reason I couldn't get my 
    # code to open until I did some searching around the internet and found that 
    # raw_input had to be replaced with input in newer versions of python. Also, I found 
    # that adding .lower() makes the user's input lower case so that it fits with the 
    # potential answers.

guesses, score = 0, 0 
# this is where the number of guesses and the number of right answers will be collected 
# and then shown to the user at the end

easy_questions = ['When you give a computer an instruction, its called 
giving a computer a _____.',
         'The _____ of a computer language is the set of rules that 
defines the combination of symbols that are considered to be a correctly 
structured document or fragment in that language.',
         'A _____ is a value, one that can change based on conditions.',
         'One piece of a larger group is called a _____ in computer 
programming.',
         '_____ are made up of elements, and are enclosed in square brackets.']

medium_questions = ['A _____ starts with the keyword def followed by the its name.',
         'A _____ is a character that represents an action, such as x representing multiplication.',
         'A _____ is a set of coded instructions that tell a computer how to run a program or calculation.',
         'A _____ is traditionally a sequence of characters either as a literal constant or as some kind of variables.',
         'An expression inside brackets is called the _____ and must be an integer value.']

hard_questions = ['A sequence of instructions that is continually repeated until a certain condition is reached, is called a _____ function.',
         'A control flow statement that allows code to be executed repeatedly based on a true/false statement is called a _____ loop.',
         'This is a common thing in programming when a variable is defined and then redefined the variable has gone through a _____.',
         '_____ refers to the situation where the same memory location can be accessed using different names']

# The first thing I tried was having all the questions and then all of the answers 
# together in two big lists bc of some code I saw online while researching how to 
# do this project but couldn't figure out how to make that code work so I started 
# over and tried this way with longer code but code I thought I could make work.

easy_answers = ['command', 'syntax', 'variable', 'element', 'lists']

medium_answers = ['function', 'operator', 'procedure', 'string', 'index']

hard_answers = ['loop', 'while', 'mutation', 'aliasing']

if level == 'easy':
    questions = easy_questions
    answers = easy_answers

elif level == 'medium':
    questions = medium_questions
    answers = medium_answers

elif level == 'hard':
    questions = hard_questions
    answers = hard_answers
    # this is how I bet thought to get the the right questions and answers to be called up.

number_of_questions = 5
question_number = 0

def check_answer(user_answer, questions, answers):

    if user_answer == answers:
        print ('')
        print ('Correct!')
        score = + 1
        guesses = + 1
        question_number =  + 1
        # this will increase the score and guesses by one and move the quiz on to 
        # the next question.
    else:
        print ('')
        print ('Not quite, please try again')
        guesses = + 1
        # this will only increase the guesses by one, and give the user another 
        # chance to answer the same question again.

while question_number < number_of_questions:
    questions = questions[question_number]
    user_answer = answers[question_number]
    print('')
    user_answer = input (questions + ': ').lower() 
    # Prompts the user to answer the fill in the blank question.

    print (check_answer(user_answer, questions, answers))
    # this is where im also having a lot of trouble, ive done some research online and this
    # is what i was told to use but it doesn't seem to be working.

    print ('')
    print ('Congratulations, you have completed the ' + str(level) + ' level, with a score of ' + str(score) + ' out of ' + str(guesses) + ' guesses')

1 个答案:

答案 0 :(得分:1)

问题似乎非常微不足道。当您使用下面的代码行检查answer中的check_answer是否正确时,

if user_answer == answers:

answers是一个包含所有答案的array,而user_answer是用户输入的字符串。因此,当您键入第一个答案为&#34;命令&#34;尝试匹配字符串&#34;命令&#34;到包含几个字符串的array。绝对不会匹配。

使用以下代码行来解决它:

if user_answer in answers: