从单独列表匹配的列表中随机输出

2024-04-25 06:49:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个单词对数字的测试,在学校进行评估,需要生成一个随机单词。我可以,但我需要一个随机数的答案,以检查它是否正确。我用四个单子来回答诸如千、百、十和单位这样的问题。然后我用从每个列表中随机选择的方法打印出字数。我有另一个名单与实际数字在和需要知道如何匹配的文字数字是与实际数字。所以如果它产生一千个,我怎么把它和1匹配,或者900个,我怎么把它和9匹配,有没有更快的方法?我可以提供我的代码,如果必要的,但它有点长。我也是个初学者

import random
wordt=['one thousand', 'two thousand', 'three thousand', 'four thousand', 'five thousand', 'six thousand', 'seven thousand', 'eight thousand', 'nine thousand']
wordh=['one hundered', 'two hundered', 'three hundered', 'four hundered', 'five hundered', 'six hundered', 'seven hundered', 'eight hundered', 'nine hundered']
wordte=['twenty', 'thirty', 'fourty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
wordu=['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
print(wordt[random.randint(0,8)], end=' ')
print(wordh[random.randint(0,8)], end=' ')
print('and', end=' ')
print(wordte[random.randint(0,6)], end=' ')
print(wordu[random.randint(0,8)])``
number=['1', '2', '3', '4', '5', '6', '7', '8', '9']

Tags: 数字randomoneendthreefourprintrandint
1条回答
网友
1楼 · 发布于 2024-04-25 06:49:27

import random

def AllSame (t):
    """
    Check if string is composed with only one, repeating character. For instance:
    >>> AllSame("aaaaaaa")
    True
    """
    if not t: return False
    l = t[0]
    c = 1
    for x in t[1:]: c += x==l
    return c==len(t)

# English numbers:
digits = {
    "1": "one", "2": "two", "3": "three", "4": "four", "5": "five", "6": "six", "7": "seven", "8": "eight", "9": "nine", "0": "zero",
    "10": "ten", "11": "eleven", "12": "twelve", "13": "thirteen", "14": "fourteen", "15": "fifteen", "16": "sixteen", "17": "seventeen", "18": "eighteen", "19": "nineteen",
    "20": "twenty", "30": "thirty", "40": "forty", "50": "fifty", "60": "sixty", "70": "seventy", "80": "eighty", "90": "ninety",
    "100": "one hundred", "200": "two hundred", "300": "three hundred", "400": "four hundred", "500": "five hundred", "600": "six hundred", "700": "seven hundred", "800": "eight hundred", "900": "nine hundred", "00": "",
    "1000": "one thousand", "2000": "two thousand", "3000": "three thousand", "4000": "four thousand", "5000": "five thousand", "6000": "six thousand", "7000": "seven thousand", "8000": "eight thousand", "9000": "nine thousand", "000": "thousand",
    "1000000": "million", "000000": "million",
    "1000000000": "billion",
    # For declanative languages. Where thousand changes form.
    # E.g. In Croatian: 1000=Tisucu 21000=Dvadeset i [jedna tisuca]
    "001": "one thousand", "002": "two thousand", "003": "three thousand", "004": "four thousand", "005": "five thousand", "006": "six thousand", "007": "seven thousand", "008": "eight thousand", "009": "nine thousand"}

def NumberToWord (num, digits=digits, lj=" ", kz=0):
    """
    Explodes a number to word(s).
    digits is a dictionary containing all major number-word mappings. See the example.
    lj is the word inserted between tens and ones.
    kz is internal for recursive control, do not use it manually.
    """
    num = num.strip()
    if kz==1:
        t = ""
        for x in num:
            if x=="0" and len(t)==0: continue
            t += x
        num = t
    if not num: return ""
    if kz==-1:
        e = ""
        for x in num: e += digits[x]+" "
        return e[:-1]
    if AllSame(num) and num[0]=="0":
        return (len(num)*(digits[num[0]]+" "))[:-1]
    if num.startswith("0"): return NumberToWord(num, digits, lj, -1)
    if digits.has_key(num): return digits[num]
    l = len(num)
    if l==2: return digits[num[0]+"0"]+lj+digits[num[1]]
    if l==3 or l==4: return NumberToWord(num[0]+((l-1)*"0"), digits, lj, 1)+" "+NumberToWord(num[1:], digits, lj, 1)
    if l==5:
        d1 = num[0]; d2 = num[1]
        if d1=="1": return (NumberToWord(num[:2], digits, lj, 1)+" "+digits["000"]+" "+NumberToWord(num[2:], digits, lj, 1)).strip()
        return (digits[d1+"0"]+" "+digits["00"+d2]+" "+NumberToWord(num[2:], digits, lj, 1)).strip()
    if l==6:
        d1 = num[0]; d2 = num[1]; d3 = num[2]
        if d2=="1": m = digits[d2+d3]+" "+digits["000"]
        else: m = digits[d2+"0"]+" "+digits["00"+d3]
        return (digits[d1+"00"]+" "+m+" "+NumberToWord(num[3:], digits, lj, 1)).strip()
    return NumberToWord(num, digits, lj, -1)

print NumberToWord(str(random.randint(0, 999999)))

相关问题 更多 >