gpt4 book ai didi

Python - 法语年份/数字发音器

转载 作者:太空宇宙 更新时间:2023-11-03 13:44:42 25 4
gpt4 key购买 nike

我想尝试用我所学的东西制作一些东西,然后想到制作一个法语年份/数字发音器(基于文本)。我偶然发现了一些问题,比如如果我有不同的数字,它们都会交换位置等。我现在提供的代码仅适用于 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"

最佳答案

因为 e = "douze" 永远不会到达。

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

我认为您在 ("7", "8", "9") 中输入了 year[2]。对于“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) )

关于Python - 法语年份/数字发音器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22598816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com