3.5 Python交互式问答应用程序答案检查不起作用
我正在尝试为此只定义一个函数 欢迎参加考试 单词1/5:土豆这个单词包含多少个consanants 三, 对 单词2/5:土豆这个单词包含多少元音 一, 对 单词3/5:说出这个单词包含多少元音 五, 不对!正确答案4 单词4/5:是的,这个单词包含多少个字母?3正确 单词5/5:天 这个词的字母3是什么 Y 对 比赛结束了。你的分数是4/5 @尼米喜欢这样吗? 随机输入 导入字符串3.5 Python交互式问答应用程序答案检查不起作用,python,python-3.x,for-loop,Python,Python 3.x,For Loop,我正在尝试为此只定义一个函数 欢迎参加考试 单词1/5:土豆这个单词包含多少个consanants 三, 对 单词2/5:土豆这个单词包含多少元音 一, 对 单词3/5:说出这个单词包含多少元音 五, 不对!正确答案4 单词4/5:是的,这个单词包含多少个字母?3正确 单词5/5:天 这个词的字母3是什么 Y 对 比赛结束了。你的分数是4/5 @尼米喜欢这样吗? 随机输入 导入字符串 def consonant_count(word): word = word.lower() r
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(int(ans), correct)
def prompt_vowel_count(word):
correct = word_map[word]['vowels']
ans = input('How many vowels does "{}" contain?'.format(word))
return check(int(ans), correct)
def prompt_consonant_count(word):
correct = word_map[word]['consonants']
ans = input('How many consonants does "{}" contain?'.format(word))
return check(int(ans), correct)
def prompt_random_letter(word):
n = random.randint(0, len(word))
correct = word[n-1]
ans = input('What is letter {} of "{}"?'.format(n, word))
return check(int(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+1, 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 = 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)
问题是,您希望返回一个
int
,而在Python3上则没有。Python3input
的工作方式与Python2上返回字符串的工作方式相同,您需要自己将其转换为其他类型。您应该能够通过在所需的位置进行转换并将所有raw\u input
调用切换到input
来修复代码,因为Python 3上没有raw\u input
例如:
def prompt_letter_count(word):
correct = word_map[word]['letters']
ans = input('How many letters does "{}" contain?'.format(word))
return check(int(ans), correct) # instead of check(ans, correct)
问题是,您希望返回一个
int
,而在Python3上则没有。Python3input
的工作方式与Python2上返回字符串的工作方式相同,您需要自己将其转换为其他类型。您应该能够通过在所需的位置进行转换并将所有raw\u input
调用切换到input
来修复代码,因为Python 3上没有raw\u input
例如:
def prompt_letter_count(word):
correct = word_map[word]['letters']
ans = input('How many letters does "{}" contain?'.format(word))
return check(int(ans), correct) # instead of check(ans, correct)
可通过调试功能检查:添加
print(repr(ans))
和print(repr(correct))
以查看一个是字符串,另一个不是字符串t@Niemmi我想我做了正确的改变,我在正确的机架上吗?看见above@SamMay否则看起来可以,但在提示\u random\u letter
时,您不想将答案转换为int
,因为您正在比较字符串之间的差异。@Niemmi因为我尝试过使用它,但字母计数似乎仍然不起作用。Wait false alarm解决了所有问题很好,谢谢mate:)@SamMay如果正确的答案类型是int
,就像所有“某物包含多少xxx?”问题一样,您需要将用户输入转换为int
。然后,如果正确的答案类型是str
,如“y字的第X个字母是什么?”中所示,则不需要进行转换,因为输入已经是str
。这可以通过调试功能check
:添加print(repr(ans))
和print(repr(correct))进行检查
查看一个是字符串,另一个不是字符串t@Niemmi我想我做了正确的改变,我在正确的机架上吗?看见above@SamMay否则看起来可以,但在提示\u random\u letter
时,您不想将答案转换为int
,因为您正在比较字符串之间的差异。@Niemmi因为我尝试过使用它,但字母计数似乎仍然不起作用。Wait false alarm解决了所有问题很好,谢谢mate:)@SamMay如果正确的答案类型是int
,就像所有“某物包含多少xxx?”问题一样,您需要将用户输入转换为int
。然后,如果正确的答案类型是str
,如“单词y的第x个字母是什么?”中所示,则不需要进行转换,因为输入已经是str
。