python 3.5中单词字母的顺序后缀。

python 3.5中单词字母的顺序后缀。,python,python-3.x,Python,Python 3.x,下面是循环的一部分,该循环从4个选项中创建随机问题,然后为这些问题指定一个单词 对于这个问题,当程序可能得到医院字母5的结果时,用户将输入答案,然后得到正确或不正确的回答。我想做的是得到字母的顺序后缀。所以问题可能是“医院的第五个字母是什么” 如果您希望转换1=>1st、2=>2nd、3=>3rd等。。。这是我能想到的最简单的方法。这种特殊情况包括1、2、3和所有以1、2或3结尾的数字,11、12和13除外 n = random.randint(1, len(x))

下面是循环的一部分,该循环从4个选项中创建随机问题,然后为这些问题指定一个单词

对于这个问题,当程序可能得到医院字母5的结果时,用户将输入答案,然后得到正确或不正确的回答。我想做的是得到字母的顺序后缀。所以问题可能是“医院的第五个字母是什么”


如果您希望转换1=>1st、2=>2nd、3=>3rd等。。。这是我能想到的最简单的方法。这种特殊情况包括1、2、3和所有以1、2或3结尾的数字,11、12和13除外

        n = random.randint(1, len(x))
        correct = x[n-1]
        if sys.version.startswith('3'):
            ans = str(input('What is letter {} of "{}"?'.format(n, x)))
        else:
            ans = str(raw_input('What is letter {} of "{}"?'.format(n, x)))

我认为0实际上是0,但不确定。您也可以使用特殊情况。

这是完整的修订代码。对于大于10的数字,显然会出现问题,可以向映射中添加数字,也可以编写一个带有规则的函数,以确定基于当前映射的正确序号后缀,但我知道原始OP不需要任何多个函数。不管怎样,这都会有所帮助

num_suffix = lambda x: "{}{}".format(x, {1: "st", 2: "nd", 3: "rd"}.get(0 if 10 > x > 14 else x % 10, "th"))

for i in range(1, 40):
    print("{} => {}".format(i, num_suffix(i))

1 => 1st
2 => 2nd
3 => 3rd
4 => 4th
5 => 5th
6 => 6th
7 => 7th
8 => 8th
9 => 9th
10 => 10th
11 => 11th
12 => 12th
13 => 13th
14 => 14th
15 => 15th
16 => 16th
17 => 17th
18 => 18th
19 => 19th
20 => 20th
21 => 21st
22 => 22nd
23 => 23rd
24 => 24th
25 => 25th
26 => 26th
27 => 27th
28 => 28th
29 => 29th
30 => 30th
31 => 31st
32 => 32nd
33 => 33rd
34 => 34th
35 => 35th
36 => 36th
37 => 37th
38 => 38th
39 => 39th

我不确定我是否理解你的问题,但你是否希望将“5”改为“5”?是的,我只是想让措辞更准确,因此,我们将调出序号后缀,以便它可以是什么是大猩猩的第5个字母?以便将来在你问这样的问题时参考,问你想要回答的具体问题。提供您解决该问题的尝试,除非要求提供其他信息。通过这种方式,你可能会得到更好/更快的答案。@sberry我觉得你在写OP的代码时并没有真正强化这一点them@jonrsharpe我理解你的观点,但我尽量避免“我该怎么做”而不显示任何代码。至少在OP的情况下,他们已经证明他们确实写了一些东西,所以他们似乎没有任何线索。不过,说得好。floccinaucinihilipilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifilifili!
num_suffix = lambda x: "{}{}".format(x, {1: "st", 2: "nd", 3: "rd"}.get(0 if 10 > x > 14 else x % 10, "th"))

for i in range(1, 40):
    print("{} => {}".format(i, num_suffix(i))

1 => 1st
2 => 2nd
3 => 3rd
4 => 4th
5 => 5th
6 => 6th
7 => 7th
8 => 8th
9 => 9th
10 => 10th
11 => 11th
12 => 12th
13 => 13th
14 => 14th
15 => 15th
16 => 16th
17 => 17th
18 => 18th
19 => 19th
20 => 20th
21 => 21st
22 => 22nd
23 => 23rd
24 => 24th
25 => 25th
26 => 26th
27 => 27th
28 => 28th
29 => 29th
30 => 30th
31 => 31st
32 => 32nd
33 => 33rd
34 => 34th
35 => 35th
36 => 36th
37 => 37th
38 => 38th
39 => 39th
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]
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}
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(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]
            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(9)