Python - 法国年/数发音人

时间:2014-03-23 23:58:54

标签: python

我想尝试做一些我学到的东西,然后考虑制作一个法国年/数字发音者(基于文本)。我偶然发现了一些问题,例如,如果我有不同的数字,他们将交换位置等。我现在提供的代码现在只适用于4位数字,但我稍后会添加其他数字。我来这里要求的是为什么如果我输入ex,脚本输出“deux”而不是“douze”。 1992年,1982年,1972年等?你会在代码的最后找到它。

由于我遗憾地没有设法正确格式化代码,我将不得不使用pastebin: 但首先我警告你;这可能会导致眼睛疼痛。我还没有理解if / elif等之间的区别。 http://pastebin.com/ZVU2N9m6

year = raw_input("Year: ")

a = "Mille"
b = ""
c = "cent"
d = ""
e = ""

if len(year) == 4: 

    # Decides century
    if year[1] == "9":
        b = " neuf "
    if year[1] == "8":
        b = " huit "
    if year[1] == "7":
        b = " sept "
    if year[1] == "6":
        b = " six "
    if year[1] == "5":
        b = " cinq "
    if year[1] == "4":
        b = " quatre "
    if year[1] == "3":
        b = " trois "
    if year[1] == "2":
        b = " deux "
    if year[1] == "1":
        b = " un "
    if year[1] == "0":
        c = ""

    # Sets decade
    if year[2] == "9":
        d = " quatre-vingt"
    if year[2] == "8":
        d = " quatre-vingt"
    if year[2] == "7":
        d = " soixante"
    if year[2] == "6":
        d = " soixante"
    if year[2] == "5":
        d = " cinquante"
    if year[2] == "4":
        d = " quarante"
    if year[2] == "3":
        d = " trente"
    if year[2] == "2":
        d = " vingt"

    # Sets year 
    if year[3] == "9":
        e = " neuf"
    if year[3] == "8":
        e = " huit"
    if year[3] == "7":
        e = " sept"
    if year[3] == "6":
        e = " six"
    if year[3] == "5":
        e = " cinq"
    if year[3] == "4":
        e = " quatre"
    if year[3] == "3":
        e = " trois"
    if year[3] == "2":
        e = " deux"
    if year[3] == "1":
        e = " et un"
    if year[3] == "0":
        e = ""

    # Sets year for 70s, 80s, 90s (different rule)
    elif year[2] == ("7", "8", "9") and year[3] == "2":
        e = "douze"

    print a + b + c + d + e

else:
    print "Your desired year does not have 4 digits"

2 个答案:

答案 0 :(得分:3)

因为永远无法访问e = "douze"

 elif year[2] == ("7", "8", "9") and year[3] == "2":
     e = "douze"

我认为你year[2] in ("7", "8", "9")。和" 82"它确实应该是" deux"而不是" douce"。


实际上,列表或词汇可以让您的生活更轻松。以下代码段生成1到99之间的法语数字名称(不包括" et"因为我不知道何时放置它):

def twoDigits (i):
    if not 0 < i < 100: raise Exception ('Out of bounds')
    below20 = [None, 'un', 'deux', 'trois', 'quatre', 'cinq', 'six', 'sept', 'huit', 'nuef',
        'dix', 'onze', 'douze', 'treize', 'quatorze', 'quinze', 'seize', 'dix-sept', 'dix-huit', 'dix-neuf']
    tens = [None, None, 'vingt', 'trente', 'quarante', 'cinquante', 'soixante', 'soixante', 'quatre-vingt', 'quatre-vingt']
    a, b = i // 10, i % 10
    if a in (1, 7, 9): b += 10
    return ' '.join (x for x in (tens [a], below20 [b] ) if x)

for a in range (1, 100):
    print (a, twoDigits (a) )

答案 1 :(得分:3)

问题是您正在检查year[2] == ("7", "8", "9")但是您需要检查year[2]是否在("7", "9")中(80秒,它不应该 douze ,检查评论)

只需更改elif,就可以解决问题:

year = raw_input("Year: ")

a = "Mille"
b = ""
c = "cent"
d = ""
e = ""

if len(year) == 4:

        # Decides century
        if year[1] == "9":
                b = " neuf "
        if year[1] == "8":
                b = " huit "
        if year[1] == "7":
                b = " sept "
        if year[1] == "6":
                b = " six "
        if year[1] == "5":
                b = " cinq "
        if year[1] == "4":
                b = " quatre "
        if year[1] == "3":
                b = " trois "
        if year[1] == "2":
                b = " deux "
        if year[1] == "1":
                b = " un "
        if year[1] == "0":
                c = ""

        # Sets decade
        if year[2] == "9":
                d = " quatre-vingt"
        if year[2] == "8":
                d = " quatre-vingt"
        if year[2] == "7":
                d = " soixante"
        if year[2] == "6":
                d = " soixante"
        if year[2] == "5":
                d = " cinquante"
        if year[2] == "4":
                d = " quarante"
        if year[2] == "3":
                d = " trente"
        if year[2] == "2":
                d = " vingt"

        # Sets year
        if year[3] == "9":
                e = " neuf"
        if year[3] == "8":
                e = " huit"
        if year[3] == "7":
                e = " sept"
        if year[3] == "6":
                e = " six"
        if year[3] == "5":
                e = " cinq"
        if year[3] == "4":
                e = " quatre"
        if year[3] == "3":
                e = " trois"
        if year[3] == "2":
                e = " deux"
        if year[3] == "1":
                e = " et un"
        if year[3] == "0":
                e = ""

        # Sets year for 70s, 80s, 90s (different rule)
        elif year[2] in ("7", "9") and year[3] == "2":
                e = " douze"

        print a + b + c + d + e

else:
        print "Your desired year does not have 4 digits"

输出:

# Year: 1992
# Mille neuf cent quatre-vingt douze

顺便说一句,我会建议你使用字典来简化,正如@AdamSmith在评论中所述