好的我一直在尝试使用输入Int函数来设置在开始时询问的问题数量,但输入似乎并不想要通过。它可以做对吗?我想要的是能够改变start_test()值。 当用户输入正确的答案时,我希望错误信息在错误后显示正确的答案。
import random
import string
import sys
# Used to refer to the what constinutes a vowel
vowels = ['a', 'e', 'i', 'o', 'u']
# Used to refer to the what constinutes consonant
consonants = [x for x in string.ascii_lowercase if x not in vowels]
#List of words used for
candidateWords = ['HELLO', 'GOODBYE', 'NAME', 'DAY', 'NIGHT', 'HOUR', 'POTATO', 'BIG', 'SMALL', 'GOOD', 'BAD', 'YES', 'NO', 'HOUSE', 'QUESTION', 'BALLOON', 'CAT', 'DUCK', 'PIGEON', 'POSTER', 'TELEVISION', 'SPY', 'RHYTHM', 'SUBSTANTIAL', 'SNOW', 'MAGNET', 'TOWEL', 'WALKING', 'SPEAKER', 'UNCHARACTERISTICALLY']
word_map = {x:{'consonants':len([y for y in x.lower() if y in consonants]), 'vowels':len([y for y in x.lower() if y in vowels]), 'letters':len(x)} for x in candidateWords}
ordinal_map = {1:'st', 2:'nd', 3:'rd', 4:'th', 5:'th', 6:'th', 7:'th', 8:'th', 9:'th', 10:'th'}
def start_test(number_questions):
current_question = 0
correct_questions = 0
if number_questions > len(candidateWords):
number_questions = len(candidateWords)
sample_questions = random.sample(candidateWords, number_questions)
print(' Welcome to Samuel Mays English Test')
print ('---------------------')
for x in sample_questions:
print ("Question {}/{}:".format(current_question+1, number_questions))
print ('---------------------')
#
current_question += 1
#q_type or question type uses a random inter between one and 4 elif is then used to set up
# what occurrs on an instance of each number in the set range
q_type = random.randint(1, 4)
if q_type == 1:
# if one is rolled the how many letter question is asked
# it refers to the word map and looks at the letters entry.
correct = word_map[x]['letters']
#Input that is presented to the user
ans = input('How many letters does "{}" contain?'.format(x))
elif q_type == 2:
# if two is rolled the how many vowel question is asked
# it refers to the word map.
correct = word_map[x]['vowels']
#Input that is presented to the user
ans = input('How many vowels does "{}" contain?'.format(x))
elif q_type == 3:
# if three is rolled the how many vowels question is asked
correct = word_map[x]['consonants']
ans = input('How many consonants does "{}" contain?'.format(x))
else:
n = random.randint(1, len(x))
correct = x[n-1]
if sys.version.startswith('3'):
ans = str(input('What is the {}{} letter of "{}"?'.format(n, ordinal_map[int(n)], x)))
else:
ans = str(raw_input('What is the {}{} letter of "{}"?'.format(n, ordinal_map[int(n)], x)))
if str(ans).lower() == str(correct).lower():
print('Well done correct! :)')
correct_questions += 1
else:
print('Wrong')
print ('You have completed your test!')
print (" Score {}/{}:".format(correct_questions , number_questions))
try_again = input('Would you like to try again? (y/n)').lower()
if try_again == 'y' or try_again == 'yes':
start_test(number_questions)
start_test(5)
弄清楚如何将定义的函数的结果放入循环
def prompt_vowel_count(word):
correct = word_map[word]['vowels']
ans = input('How many vowels does "{}" contain?'.format(word))
return check(int(ans), correct)
答案 0 :(得分:2)
import random
import string
import sys
vowels = ['a', 'e', 'i', 'o', 'u']
consonants = [x for x in string.ascii_lowercase if x not in vowels]
candidateWords = ['HELLO', 'GOODBYE', 'NAME', 'DAY', 'NIGHT', 'HOUR', 'POTATO', 'BIG', 'SMALL', 'GOOD', 'BAD', 'YES', 'NO', 'HOUSE', 'QUESTION', 'BALLOON', 'CAT', 'DUCK', 'PIGEON', 'POSTER', 'TELEVISION', 'SPY', 'RHYTHM', 'SUBSTANTIAL', 'SNOW', 'MAGNET', 'TOWEL', 'WALKING', 'SPEAKER', 'UNCHARACTERISTICALLY']
word_map = {x:{'consonants':len([y for y in x.lower() if y in consonants]), 'vowels':len([y for y in x.lower() if y in vowels]), 'letters':len(x)} for x in candidateWords}
ordinal_map = {1:'st', 2:'nd', 3:'rd', 4:'th', 5:'th', 6:'th', 7:'th', 8:'th', 9:'th', 10:'th'}
def prompt_vowel_count(word):
correct = word_map[word]['vowels']
ans = input('How many vowels does "{}" contain?'.format(word))
return correct, ans
def start_test(number_questions):
current_question = 0
correct_questions = 0
if number_questions > len(candidateWords):
number_questions = len(candidateWords)
sample_questions = random.sample(candidateWords, number_questions)
print(' Welcome to Year One Greens English Test')
print ('---------------------')
for x in sample_questions:
print ("Question {}/{}:".format(current_question+1, number_questions))
print ('---------------------')
current_question += 1
#q_type or question type uses a random inter between one and 4 elif is then used to set up
q_type = random.randint(1, 4)
if q_type == 1:
correct = word_map[x]['letters']
ans = input('How many letters does "{}" contain?'.format(x))
elif q_type == 2:
correct, ans = prompt_vowel_count(x)
elif q_type == 3:
correct = word_map[x]['consonants']
ans = input('How many consonants does "{}" contain?'.format(x))
else:
n = random.randint(1, len(x))
correct = x[n-1]
if sys.version.startswith('3'):
ans = str(input('What is the {}{} letter of "{}"?'.format(n, ordinal_map[int(n)], x)))
else:
ans = str(raw_input('What is the {}{} letter of "{}"?'.format(n, ordinal_map[int(n)], x)))
if str(ans).lower() == str(correct).lower():
print('Well done correct! :)')
correct_questions += 1
else:
print('Wrong')
print ('You have completed your test!')
print (" Score {}/{}:".format(correct_questions , number_questions))
try_again = input('Would you like to try again? (y/n)').lower()
if try_again == 'y' or try_again == 'yes':
start_test(number_questions)
start_test(5)