gpt4 book ai didi

python - for 循环 Python 3.5。为我辅导的学生创建测验

转载 作者:太空宇宙 更新时间:2023-11-03 16:12:12 26 4
gpt4 key购买 nike

您好,我目前正在尝试为一年级英语学生制作测验申请。我正在使用Python 3.5。我是个新人。测验应该像下面一样工作,我也会包含代码。

WELCOME TO YOUR QUIZ

Word 1/5: Potato How many consanants does the word contain?

3

Correct!

Word 2/5: Potato How many vowels does the word contain?

1

Correct!

Word 3/5: Name How many vowels does the word contain

5

Incorrect! Correct answer 4

Word 4/5: YES How many letters does the word contain? 3 Correct!

Word 5/5: Day

What is letter 3 of the word?

Y

Correct!

Game Over. Your Score is 4/5

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)
# loop through the list using enumerate
for index,w in enumerate(s):
print("Word {}/{}:{}".format(index+1,len(s),w))

我想生成一个 1 到 4 之间的随机数,并用它来选择每个单词要问的问题。

如果随机数是1,我想问用户“这个单词包含多少个字母?”并提示他们输入。评估他们的答案,然后打印适当的消息。我该如何将其融入其中?

最佳答案

这就是我创建测验的方式。显然,您可以更新打印语句以遵循您自己想要的格式。希望这有帮助!

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 - for 循环 Python 3.5。为我辅导的学生创建测验,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39181543/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com