[英]A simple Quiz in python
我正在编写一个 python 代码来用英语和新鲜的方式训练用户,根据选项,它会询问英语或法语问题并接受答案,然后它会根据列表检查输入的答案并增加分数,但是在第一个问题,我的代码停止运行
我试过包含一个 for 循环并使 while 循环成为函数的一部分,但什么都没有
#Python program to drill a student in french
import sys
option = 0
score=0
english_questions=['What is Thank you in French___?', 'What is you are welcome in French___?', 'What is no in French___?', 'What is Pardon in French___?', 'What is yes in French?___?']
french_questions =['What does Merci mean in English___? ', 'What does derien mean in English___?','What does No mean in English___?', 'What does pardon mean in English___?', 'What does oui mean in English___?' ]
french_answers =['merci', 'derien', 'no', 'pardon', 'oui']
english_answers=['thankyou', 'welcome', 'no', 'pardon', 'yes']
number_of_questions = 5
question_number = 0
print('Welcome to English-French Vocabulary Drill')
print('*********************************************')
print('To be drilled in English Press 1')
print('To be drilled in French Press 2')
print('*********************************************')
#a try except block to handle invalid option type
if option not in (1, 2):
try:
option=input('Please Enter option:')
except:
print('Invalid option, Please enter 1 or 2')
if option == 1:
questions = english_questions
answers = french_answers
elif option == 2:
questions = french_questions
answers = english_answers
#Function to check answer
def check_answer(user_answer, questions, answers):
if user_answer in answers:
print('')
print('Correct')
global score
score +=1
global question_number
question_number +=1
else:
print('')
print('Incorrect, try again')
global guesses
guesses +=1
global number_of_questions
while question_number < number_of_questions:
x = questions[question_number]
user_answer = answers[question_number]
print('')
user_answer = input (x + ':')
print (check_answer(user_answer, x, answers))
print('')
print('score : ' +str(score) )
我希望它打印出每个问题并一个接一个地请求答案,但我得到的是“正确无得分:0 W:”
此处: questions = questions[question_number]
,您为questions
分配questions
列表的值。 当循环第二次执行时,您将收到一封信,因为您将要求程序查找questions[question_number][question_number]
。 也许您应该尝试使用不同的变量名称?
除了 Rasgel 的回答之外,您应该将所有a =+ b
语句更改为a += b
以进行适当的递增。
我稍微更正了代码。 有很多错误。
import sys
score = 0
guesses= 0
english_questions=['What is Thank you in French___?', 'What is you are welcome in French___?', 'What is no in French___?', 'What is Pardon in French___?', 'What is yes in French?___?']
french_questions =['What does Merci mean in English___? ', 'What does derien mean in English___?','What does No mean in English___?', 'What does pardon mean in English___?', 'What does oui mean in English___?' ]
french_answers =['merci', 'derien', 'no', 'pardon', 'oui']
english_answers=['thankyou', 'welcome', 'no', 'pardon', 'yes']
number_of_questions = 5
question_number = 0
print('Welcome to English-French Vocabulary Drill')
print('*********************************************')
print('To be drilled in English Press 1')
print('To be drilled in French Press 2')
print('*********************************************')
#a try except block to handle invalid option type
option= int(input('Please Enter option:'))
while option not in (1,2) and type(option) == int:
option = int(input("please enter valid option"))
if option == 1:
questions = english_questions
answers = french_answers
elif option == 2:
questions = french_questions
answers = english_answers
#Function to check answer
def check_answer(user_answer, answers):
global question_number
if user_answer in answers:
print('')
print('Correct')
global score
score += 1
else:
print('')
print('Incorrect, try again')
global guesses
guesses = guesses+ 1
question_number = question_number + 1
while question_number < number_of_questions:
cur_question = questions[question_number]
user_answer = answers[question_number]
print('')
user_answer = input (cur_question + ':')
print (check_answer(user_answer, answers))
print('')
print('score : ' +str(score) )
在脚本顶部声明并初始化这些变量:
number_of_questions = 0
guesses = 0
然后将选项与字符串进行比较,这是用户输入:
if option == '1':
questions = english_questions
answers = french_answers
elif option == '2':
questions = french_questions
answers = english_answers
通过这些修复,它可以在不提升的情况下运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.