Python3.5 for循环中放置的循环定义函数

Python3.5 for循环中放置的循环定义函数,python,python-3.x,python-3.5,Python,Python 3.x,Python 3.5,好的,我一直在尝试使用input 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 t

好的,我一直在尝试使用input 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)

你能发布你的错误信息吗?下面是什么“循环”?我看到的唯一循环是示例问题中x的
。我记得这是为您写的,问题是什么?您发布的代码没有编译。你说的是哪个环路?这是样本问题中x的
?这似乎没有任何意义。我认为你需要停下来好好想想你想问什么,看看你是否能和我们沟通。为什么您必须更改代码以摆脱函数?是因为你的老师让你这么做的吗?@jwpfox你知道它是如何生成一个随机数,然后引出一个与这个数相关的问题的,例如是1,然后创建一个字母计数问题,我想把发生的事情放在def-question\u-letter\u-count(word):correct=word\u-map[word]['letters']ans=input(“{}”包含多少个字母?”。格式(word))返回检查(int(ans),更正到循环体中。我不确定你是否真的通过不断为OP编写代码来帮助OP。@LazyScript感谢mate I,但我已经有了,我会将桌面上的内容重新粘贴到顶部面板上。Accor对我为之做这个的人来说,我只需要一个定义的函数。def question_letter_count(word):correct=word\u map[word]['letters']ans=input(“{}”包含多少个字母?”。format(word))返回检查(int(ans),correct)我需要把它转移到设置test@TheLazyScripter我不同意,当已经有一个raw_输入函数返回字符串而不必强制转换任何内容时,您已经在一个已评估的输入上发布了str强制转换。因此,这是一个非常糟糕的答案,它没有任何作用t帮助OP编辑:我没有看到python的版本。我对原始输入信息表示歉意。@Almasyx OP使用3.5清楚地定义了问题。因此,
raw\u input
已被
input
替换,
input
已被删除。我键入cast作为字符串提醒程序员所需的输入是是的,我没有看到python版本。很抱歉。
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)