尝试使用 python 2.7 填写空白测验,但程序没有按照我想要的方式执行


print 'Welcome to General Quiz, a quiz where you are asked to select from three difficulty levels, with each level containing 5 fill in the blank questions'
print ''
print 'Rules:'
print '- These questions will be general knowledge based'
print '- Your score, which will be shown in the end, will be based on how many correct answers you get compared to the number of guesses you take' 
print '- It will not matter if you type anything lower or upper case'
print '- Some answers will require multiple words, so make sure you include spaces'
print '- For numbered answers, sumbit answers in numbers, not words'
print'' # These past few lines simply print out the rules for the user when they play this game
levelselect = 'no'# This variable will only set a condition for the following while loop
while levelselect == 'no':
print ''
level = raw_input ('Please select a difficulty (easy, medium, or hard): ').lower()# Prompts the user to select a difficulty and makes all letters lower case so it won't matter what case the user types in their input
if level == 'easy' or level == 'medium' or level == 'hard':
break # If the user's input is one of these three words, the program will break out of this loop, regardless of the value of the variable 'levelselect'
guesses = 0 # Used to record the number of guesses from the user to be displayed at the end of the game
score = 0 # Used to record the number of correct guesses the user gets correct later on in the game
print''
easy_questions = ['The chemical symbol (O) means _____',
'_____  is the best hockey player of all time',
'_____ is the year Canada became an independant country',
'The song "Sorry" was sung by _____',
'Steve Jobs is one the founders of  _____ ']
medium_questions = ['_____ is Canadas very first capital',
'The national team of _____ won the 2014 world cup',
'In a computer, RAM stands for _____',
'Your kidneys and your _____ are the organs that filter blood in your body',
'In Breaking Bad, the wife of Walter is _____ White']
hard_questions = ['In NASAs apollo program, _____ of their missions successfully landed men on the moom',         
'_____ was the year the NFL was founded',
'The first film to gross over one $1 billion is _____',
'_____ is the business with the worlds largest revenue',
'Lego was invented in the country of _____'] # This array contains all the answers with the blanks that are to be filled in
easy_answers = ['oxygen', 'wayne gretzky', '1867', 'justin bieber',
'apple']
medium_answers = ['kingston', 'germany', 'random access memory',
'liver', 'skyler']
hard_answers = ['6', '1920', 'titanic', 'walmart', 'denmark'] # This array contains all the answers, with corresponding indexes to the previous array
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
totalQuestions = 5 # The questions array contains 15 questions, with the first 5 being easy, the next 5 being medium etc. and this helps to identify the finishing point of the asking of questions
questionNum = 0
def check_answer(user_answer, system_question, system_answer):
if user_answer == system_answer:
return ''
return ('Correct!')
return system_question.replace ('_____', system_answer) # Shows question with the blank filled with the correct answer
score += 1 # Increases user's score by 1
guesses += 1 # Increases user's guesses by 1
questionNum += 1 # Makes the code in this while loop move onto the next question in the questions array and the next answer in the answers array
else:
return ''
return ('Incorrect, Try Again')
guesses += 1
while questionNum < totalQuestions:
Question = questions[questionNum]
Answer = answers[questionNum]
print ''
answer = raw_input (Question + ': ').lower() # Prompts the user to answer the fill in the blank question
print check_answer(answer, Question, Answer)
print ''
print ('Congratulations, you have completed the ' + str(level) + ' level, with a score of ' +
str(score) + ' out of ' + str(guesses) + ' guesses') 

对于在线课程,我正在尝试填写空白测验,该测验应该要求用户选择一个难度,每个难度由 5 个问题组成。当用户猜对时,必须以正确答案显示答案,否则将提示用户再次回答。该程序要么没有按照我想要的方式执行,要么有错误

我认为这可能会有所帮助。稍微更改了代码。

print '''Welcome to General Quiz, a quiz where you are asked to select from three difficulty levels, with each level 
containing 5 fill in the blank questions'             
nRules:
n- These questions will be general knowledge based
n- Your score, which will be shown in the end, will be based on how many correct 
answers you get compared to the number of guesses you take 
n- It will not matter if you type anything lower or upper case print
n- Some answers will require multiple words, so make sure you include spaces
n- For numbered answers, sumbit answers in numbers, not words'''
level=None
while level not in ['easy', 'medium', 'hard']:
level = raw_input ('Please select a difficulty (easy, medium, or hard): ').lower()
guesses, score = 0, 0
questions = ['The chemical symbol (O) means _____',
'_____  is the best hockey player of all time',
'_____ is the year Canada became an independant country',
'The song "Sorry" was sung by _____',
'Steve Jobs is one the founders of  _____ ',
'_____ is Canadas very first capital',
'The national team of _____ won the 2014 world cup',
'In a computer, RAM stands for _____',
'Your kidneys and your _____ are the organs that filter blood in your body',
'In Breaking Bad, the wife of Walter is _____ White',
'In NASAs apollo program, _____ of their missions successfully landed men on the moom',
'_____ was the year the NFL was founded',
'The first film to gross over one $1 billion is _____',
'_____ is the business with the worlds largest revenue',
'Lego was invented in the country of _____']
answers = ['oxygen', 'wayne gretzky', '1867', 'justin bieber', 'apple', 'kingston', 'germany', 'random access memory',
'liver', 'skyler', '6', '1920', 'titanic', 'walmart', 'denmark']
dict = {'easy':0, 'medium':5, 'hard':10}
for i in xrange(dict[level], dict[level]+5):
answer = raw_input(questions[i] + ': ').lower()
while(answer != answers[i]):
print('Incorrect, Try Again')
guesses = guesses+1
answer = raw_input(questions[i] + ': ').lower()
print('Correct n'+ questions[i].replace('_____',answer))
score, guesses = score + 1, guesses+1
print ('Congratulations, you have completed the ' + str(level) + ' level, with a score of ' +
str(score) + ' out of ' + str(guesses) + ' guesses')

最新更新