daniel wong daniel wong - 2 months ago 9
Python Question

python 3.5 for loop defined functions placed in the loop

Apparently I have to transfer the functions below into the loop below them and only have the vowel function there working. In short I need to make the loop perform all the functions of the other def functions is there a way to transfer them across without changing them in a massive manner? I'm running on two hours of sleep here. Sorry for grammar issues.

import random
import string

def question_letter_count(word):
correct = word_map[word]['letters']
ans = input('How many letters does "{}" contain?'.format(word))
return check(int(ans), correct)

def question_count(word):
correct = word_map[word]['vowels']
ans = input('How many vowels does "{}" contain?'.format(word))
return check(int(ans), correct)

def question_consonant_count(word):
correct = word_map[word]['consonants']
ans = input('How many consonants does "{}" contain?'.format(word))
return check(int(ans), correct)

def question_random_letter(word):
n = random.randint(1, len(word))
correct = word[n-1]
ans = input('What is letter {} of "{}"?'.format(n, word))
return check(ans.lower(), correct.lower())

def check(ans, correct):
if ans == correct:
return show_correct()
return show_incorrect()

def show_correct():
print('Well done correct! :)')
return 1

def show_incorrect():
print('Wrong :(')
return 0

def next_question(word):
q_type = input_map[random.randint(1, 8)]
return q_type(word)

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])

vowels = ['a', 'e', 'i', 'o', 'u']
consonants = [x for x in string.ascii_lowercase if x not in vowels]
SelectedWords = ['TRAILER', 'LAPTOP', 'SUNFLOWER', 'SKIPPING', 'AIRPLANE', 'HOUR', 'POTATO', 'HUGE', 'TINY', 'GOOD', 'BAD', 'YES', 'NO', 'WAGON', 'QUESTION', 'LAGOON', 'CAT', 'DUCK', 'GOANNA', 'POSTER', 'FUTURE', 'PRINCESS', 'RHYTHM', 'SUDDENLY', 'SNOW', 'MAGNET', 'TOWEL', 'RUNNING', 'SPEAKER', 'QUICKLY']
word_map = {x:{'consonants':consonant_count(x), 'vowels':vowel_count(x), 'letters':len(x)} for x in SelectedWords}
input_map = {1:question_letter_count, 2:question_vowel_count, 3:question_consonant_count, 4:question_random_letter}

def start_test(number_questions):
current_question = 0
correct_questions = 0
if number_questions > len(SelectedWords):
number_questions = len(SelectedWords)
sample_questions = random.sample(SelectedWords, number_questions)
print(' Test 1 START')
print ('---------------------')
for x in sample_questions:
print ("Question {}/{}:".format(current_question+1, number_questions))
correct_questions += next_question(x)
print ('---------------------')
current_question += 1
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(9)


When the above code is compiled I receive this error message:

Traceback (most recent call last):
File "<filepath>", line 56, in <module>
input_map = {1:question_letter_count, 2:question_vowel_count, 3:question_consonant_count, 4:question_random_letter}
NameError: name 'question_vowel_count' is not defined

Answer

Here you go.

import random
import string

def question_letter_count(word):
    correct = word_map[word]['letters']
    ans = input('How many letters does "{}" contain?'.format(word))
    return check(int(ans), correct)

def question_vowel_count(word):
    correct = word_map[word]['vowels']
    ans = input('How many vowels does "{}" contain?'.format(word))
    return check(int(ans), correct)

def question_consonant_count(word):
    correct = word_map[word]['consonants']
    ans = input('How many consonants does "{}" contain?'.format(word))
    return check(int(ans), correct)

def question_random_letter(word):
    n = random.randint(1, len(word))
    correct = word[n-1]
    ans = str(input('What is letter {} of "{}"?'.format(n, word))).lower()
    return check(ans.lower(), correct.lower())

def check(ans, correct):
    if ans == correct:
        return show_correct()
    return show_incorrect()


def show_correct():
    print('Well done correct! :)')
    return 1

def show_incorrect():
    print('Wrong :(')
    return 0

def next_question(word):
    q_type = input_map[random.randint(1, 4)]
    return q_type(word)

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])

vowels = ['a', 'e', 'i', 'o', 'u']
consonants = [x for x in string.ascii_lowercase if x not in vowels]
SelectedWords = ['TRAILER', 'LAPTOP', 'SUNFLOWER', 'SKIPPING', 'AIRPLANE', 'HOUR', 'POTATO', 'HUGE', 'TINY', 'GOOD', 'BAD', 'YES', 'NO', 'WAGON', 'QUESTION', 'LAGOON', 'CAT', 'DUCK', 'GOANNA', 'POSTER', 'FUTURE', 'PRINCESS', 'RHYTHM', 'SUDDENLY', 'SNOW', 'MAGNET', 'TOWEL', 'RUNNING', 'SPEAKER', 'QUICKLY']
word_map = {x:{'consonants':consonant_count(x), 'vowels':vowel_count(x), 'letters':len(x)} for x in SelectedWords}
input_map = {1:question_letter_count, 2:question_vowel_count, 3:question_consonant_count, 4:question_random_letter}

def start_test(number_questions):
    current_question = 0
    correct_questions = 0
    if number_questions > len(SelectedWords):
        number_questions = len(SelectedWords)
    sample_questions = random.sample(SelectedWords, number_questions)
    print sample_questions
    print(' Test 1 START')
    print ('---------------------')
    for x in sample_questions:
        print ("Question {}/{}:".format(current_question+1,     number_questions))
        correct_questions += next_question(x)
        print ('---------------------')
        current_question += 1
    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(9)

Hopefully this is what you were looking for?

import random
import string

vowels = ['a', 'e', 'i', 'o', 'u']
consonants = [x for x in string.ascii_lowercase if x not in vowels]
SelectedWords = ['TRAILER', 'LAPTOP', 'SUNFLOWER', 'SKIPPING', 'AIRPLANE', 'HOUR', 'POTATO', 'HUGE', 'TINY', 'GOOD', 'BAD', 'YES', 'NO', 'WAGON', 'QUESTION', 'LAGOON', 'CAT', 'DUCK', 'GOANNA', 'POSTER', 'FUTURE', 'PRINCESS', 'RHYTHM', 'SUDDENLY', 'SNOW', 'MAGNET', 'TOWEL', 'RUNNING', 'SPEAKER', 'QUICKLY']
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 SelectedWords}

def start_test(number_questions):
    current_question = 0
    correct_questions = 0
    if number_questions > len(SelectedWords):
        number_questions = len(SelectedWords)
    sample_questions = random.sample(SelectedWords, number_questions)
    print sample_questions
    print(' Test 1 START')
    print ('---------------------')
    for x in sample_questions:
        print ("Question {}/{}:".format(current_question+1,     number_questions))
        print ('---------------------')
        current_question += 1
        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 = word_map[x]['vowels']
            ans = input('How many vowels does "{}" contain?'.format(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]
            ans = str(input('What is letter {} of "{}"?'.format(n, x))).lower()
        print correct
        if ans == correct:
            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(9)
Comments