对于循环Python 3.5。为我辅导的学生创建测验
您好,我目前正在尝试为一年级的英语学生做一个测验申请。我正在使用Python 3.5。我是新来的。测验应该像下面那样工作,我也会包括代码 欢迎参加你的测验 单词1/5:土豆这个单词包含多少个consanants 三, 对 单词2/5:土豆这个单词包含多少元音 一, 对 单词3/5:说出这个单词包含多少元音 五, 不对!正确答案4 单词4/5:是的,这个单词包含多少个字母?3正确 单词5/5:天 这个词的字母3是什么 Y 对 比赛结束了。你的分数是4/5 我想生成一个介于1和4之间的随机数,并用它来选择每个单词要问的问题对于循环Python 3.5。为我辅导的学生创建测验,python,python-3.x,for-loop,Python,Python 3.x,For Loop,您好,我目前正在尝试为一年级的英语学生做一个测验申请。我正在使用Python 3.5。我是新来的。测验应该像下面那样工作,我也会包括代码 欢迎参加你的测验 单词1/5:土豆这个单词包含多少个consanants 三, 对 单词2/5:土豆这个单词包含多少元音 一, 对 单词3/5:说出这个单词包含多少元音 五, 不对!正确答案4 单词4/5:是的,这个单词包含多少个字母?3正确 单词5/5:天 这个词的字母3是什么 Y 对 比赛结束了。你的分数是4/5 我想生成一个介于1和4之间的随机数,并用它
如果随机数是1,我想问用户“这个单词包含多少个字母?”并提示他们输入。评估他们的答案,然后打印适当的消息。我将如何适应这一点?我提出了一种选择问题并关联测试功能的方法,该功能允许轻松定义任意数量的问题:
print('WELCOME TO YOUR QUIZ')
# Import the random module to allow us to select the word list and questions at random.
import random
quizWords = ['WOMBAT', 'COMPUTER', 'BOOKS', 'DAY', 'NIGHT', 'HOUR', 'POTATO', 'HUGE', 'TINY', 'GOOD', 'BAD', 'YES', 'NO', 'HOUSE', 'QUESTION', 'BALLOON', 'CAT', 'DUCK', 'KOOKARBURRA', 'POSTER', 'TELEVISION', 'PRINCE', 'RHYTHM', 'SUDDENLY', 'SNOW', 'MAGNET', 'TOWEL', 'WALKING', 'SPEAKER', 'UNCHARACTERISTICALLY']
s = random.sample (quizWords, 5)
def test_nb_vows(word):
return sum(word.upper().count(x) for x in "AEIOUY")
qt = {"How many vowels does the word contain?" : test_nb_vows, "How many letters does the word contain?" : len}
# loop through the list using enumerate
for index,w in enumerate(s):
print("Word {}/{}:{}".format(index+1,len(s),w))
# check question
qk = random.choice(list(qt.keys()))
answer = int(input(qk))
real_answer = qt[qk](w)
if real_answer == answer:
print("Correct!")
else:
print("Noooo it is {}".format(real_answer))
其思想是使用random.choice
随机选择问题。选择是字典的一个键,值是test函数,它必须有1个参数:单词本身,并返回正确答案(现在限制为一个数字),根据用户输入进行测试
这样,只要提供相关的测试功能,就可以定义许多问题。您只需充实
qt
并编写测试函数(或者像我那样使用现有函数,重用len
)。祝你好运。这里有一个可能的解决方案:
# Import the random module to allow us to select the word list and
# questions at random.
import random
quizWords = [
'WOMBAT', 'COMPUTER', 'BOOKS', 'DAY', 'NIGHT', 'HOUR', 'POTATO', 'HUGE',
'TINY', 'GOOD', 'BAD', 'YES', 'NO', 'HOUSE', 'QUESTION', 'BALLOON',
'CAT', 'DUCK', 'KOOKARBURRA', 'POSTER', 'TELEVISION', 'PRINCE', 'RHYTHM',
'SUDDENLY', 'SNOW', 'MAGNET', 'TOWEL', 'WALKING', 'SPEAKER',
'UNCHARACTERISTICALLY'
]
s = random.sample(quizWords, 5)
def f0(word):
""" TODO """
return True
def f1(word):
""" TODO """
return True
def f2(word):
""" TODO """
return True
def f3(word):
""" TODO """
return True
def f4(word):
""" TODO """
return True
questions = {
0: {
"title": "How many consanants does the word {0} contain?",
"f": f0
},
1: {
"title": "How many vowels does the word {0} contain?",
"f": f1
},
2: {
"title": "How many vowels does the word {0} contain",
"f": f2
},
3: {
"title": "How many letters does the word {0} contain?",
"f": f3
},
4: {
"title": "What is letter 3 of the word {0}?",
"f": f4
}
}
# loop through the list using enumerate
for index, w in enumerate(s):
print("Word {}/{}:{}".format(index + 1, len(s), w))
type_question = random.randint(0, 4)
print("{}".format(questions[type_question]["title"].format(w)))
questions[type_question]["f"](w)
如您所见,您只需使用random.randint从问题字典中提取一个随机问题。现在您只需要完成函数f0..f4;-)的逻辑。希望有帮助 这就是我创建测验的方法。显然,您可以更新print语句以遵循您自己想要的格式。希望这有帮助
import random
import string
def consonant_count(word):
word = word.lower()
return len([x for x in word if x in consonants])
def vowel_count(word):
word = word.lower()
return len([x for x in word if x in vowels])
def prompt_letter_count(word):
correct = word_map[word]['letters']
ans = input('How many letters does "{}" contain?'.format(word))
return check(ans, correct)
def prompt_vowel_count(word):
correct = word_map[word]['vowels']
ans = input('How many vowels does "{}" contain?'.format(word))
return check(ans, correct)
def prompt_consonant_count(word):
correct = word_map[word]['consonants']
ans = input('How many consonants does "{}" contain?'.format(word))
return check(ans, correct)
def prompt_random_letter(word):
n = random.randint(0, len(word))
correct = word[n-1]
ans = raw_input('What is letter {} of "{}"?'.format(n, word))
return check(ans.lower(), correct.lower())
def check(ans, correct):
if ans == correct:
return prompt_correct()
return prompt_incorrect()
def prompt_correct():
print('That is correct! :)')
return 1
def prompt_incorrect():
print('That is incorrect :(')
return 0
def next_question(word):
q_type = input_map[random.randint(1, 4)]
return q_type(word)
vowels = ['a', 'e', 'i', 'o', 'u']
consonants = [x for x in string.ascii_lowercase if x not in vowels]
quizWords = ['WOMBAT', 'COMPUTER', 'BOOKS', 'DAY', 'NIGHT', 'HOUR', 'POTATO', 'HUGE', 'TINY', 'GOOD', 'BAD', 'YES', 'NO', 'HOUSE', 'QUESTION', 'BALLOON', 'CAT', 'DUCK', 'KOOKARBURRA', 'POSTER', 'TELEVISION', 'PRINCE', 'RHYTHM', 'SUDDENLY', 'SNOW', 'MAGNET', 'TOWEL', 'WALKING', 'SPEAKER', 'UNCHARACTERISTICALLY']
word_map = {x:{'consonants':consonant_count(x), 'vowels':vowel_count(x), 'letters':len(x)} for x in quizWords}
input_map = {1:prompt_letter_count, 2:prompt_vowel_count, 3:prompt_consonant_count, 4:prompt_random_letter}
def start_quiz(number_questions):
current_question = 0
correct_questions = 0
if number_questions > len(quizWords):
number_questions = len(quizWords)
sample_questions = random.sample(quizWords, number_questions)
print('WELCOME TO YOUR QUIZ')
print '---------------------'
for x in sample_questions:
print 'Question {}/{}:'.format(current_question, number_questions)
correct_questions += next_question(x)
print '---------------------'
current_question += 1
print 'Congragulations on completing your quiz!'
print " Score {}/{}:".format(correct_questions, number_questions)
try_again = raw_input('Would you like to try again? (y/n)').lower()
if try_again == 'y' or try_again == 'yes':
start_quiz(number_questions)
start_quiz(4)
谢谢,我会看看它是如何在python上运行的,谢谢你的建议。伙计,当你刚开始的时候,这个社区真的很棒。谢谢,我会用python来看看。大量帮助guys@MikeLyn不客气,但有一件事,在这里表示感谢的最好方式是验证或更新您认为对您最有帮助的答案,这是游戏规则;)在这个网站上,通过投票选出所有有用的答案来表达你的感激之情。您可以通过单击答案左上角的向上箭头来完成此操作。此外,单击答案左上角附近的复选标记接受最佳答案。这比在评论中说谢谢要好。这是python 3.5吗?似乎不想在3.5中运行
import random
import string
def consonant_count(word):
word = word.lower()
return len([x for x in word if x in consonants])
def vowel_count(word):
word = word.lower()
return len([x for x in word if x in vowels])
def prompt_letter_count(word):
correct = word_map[word]['letters']
ans = input('How many letters does "{}" contain?'.format(word))
return check(ans, correct)
def prompt_vowel_count(word):
correct = word_map[word]['vowels']
ans = input('How many vowels does "{}" contain?'.format(word))
return check(ans, correct)
def prompt_consonant_count(word):
correct = word_map[word]['consonants']
ans = input('How many consonants does "{}" contain?'.format(word))
return check(ans, correct)
def prompt_random_letter(word):
n = random.randint(0, len(word))
correct = word[n-1]
ans = raw_input('What is letter {} of "{}"?'.format(n, word))
return check(ans.lower(), correct.lower())
def check(ans, correct):
if ans == correct:
return prompt_correct()
return prompt_incorrect()
def prompt_correct():
print('That is correct! :)')
return 1
def prompt_incorrect():
print('That is incorrect :(')
return 0
def next_question(word):
q_type = input_map[random.randint(1, 4)]
return q_type(word)
vowels = ['a', 'e', 'i', 'o', 'u']
consonants = [x for x in string.ascii_lowercase if x not in vowels]
quizWords = ['WOMBAT', 'COMPUTER', 'BOOKS', 'DAY', 'NIGHT', 'HOUR', 'POTATO', 'HUGE', 'TINY', 'GOOD', 'BAD', 'YES', 'NO', 'HOUSE', 'QUESTION', 'BALLOON', 'CAT', 'DUCK', 'KOOKARBURRA', 'POSTER', 'TELEVISION', 'PRINCE', 'RHYTHM', 'SUDDENLY', 'SNOW', 'MAGNET', 'TOWEL', 'WALKING', 'SPEAKER', 'UNCHARACTERISTICALLY']
word_map = {x:{'consonants':consonant_count(x), 'vowels':vowel_count(x), 'letters':len(x)} for x in quizWords}
input_map = {1:prompt_letter_count, 2:prompt_vowel_count, 3:prompt_consonant_count, 4:prompt_random_letter}
def start_quiz(number_questions):
current_question = 0
correct_questions = 0
if number_questions > len(quizWords):
number_questions = len(quizWords)
sample_questions = random.sample(quizWords, number_questions)
print('WELCOME TO YOUR QUIZ')
print '---------------------'
for x in sample_questions:
print 'Question {}/{}:'.format(current_question, number_questions)
correct_questions += next_question(x)
print '---------------------'
current_question += 1
print 'Congragulations on completing your quiz!'
print " Score {}/{}:".format(correct_questions, number_questions)
try_again = raw_input('Would you like to try again? (y/n)').lower()
if try_again == 'y' or try_again == 'yes':
start_quiz(number_questions)
start_quiz(4)