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}
以下是循环的一部分,该循环从4个选项中创建随机问题,然后为这些问题分配一个单词。
对于这个问题,当程序可能得到医院字母5的结果时,用户会输入答案,然后得到正确或不正确的答案。我正在尝试做的是获得该字母的序数后缀。所以问题可能是“什么是医院的第5个字母”
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 :(得分:2)
如果您要转换1 => 1,2,> 2,3 => 3,等等......这是我能想到的最简单的方法。除了11,12和13之外,这个特殊情况1,2,3和所有数字都以1,2或3结尾。
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
我认为0实际上是0,但不确定。你也可以这样特殊。
答案 1 :(得分:0)
以下是完整修订后的代码。显然存在大于10的数字的问题,可以向地图添加数字或编写带有规则的函数来确定基于当前地图的正确序数后缀,但我知道原始OP没有不想要一个以上的功能。无论哪种方式,这都应该有所帮助。
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)