簡體   English   中英

一個簡單的python測驗

[英]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以進行適當的遞增。

我稍微更正了代碼。 有很多錯誤。

  • 您需要創建在代碼中隨處使用的全局變量,例如 score 和 guesses
  • 您需要將 questions[question_number] 存儲在一個新變量中。
  • 您需要添加 while 循環來檢查不正確的輸入。
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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM