我想用法语编一个动词的共轭码,但我不能把键“je”改成“j”

2024-06-09 21:45:59 发布

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

我试图让我的代码像这样工作:

Enter a verb in French: chanter

Output 1: je chante
          tu chantes
          il ou elle chante
          nous chantons
          vous chantez
          ils ou elles chantent

我成功地完成了上面的部分,但是当用户进入时,我无法成功地将je切换到j',例如:echapper

Enter a verb in French: echapper

Output 2: j'echappe
          tu echappes
          il ou elle echappe
          nous echappons
          vous echappez
          ils ou elles echappent

代码:

list = {
    "je": 'e',
    "tu": 'es',
    "il ou elle": 'e',
    "nous": 'ons',
    "vous": 'ez',
    "ils ou elles": 'ent'
}

veb = input("")

for key in list:
    if veb.endswith('er'):
        b = veb[:-2]
        print(key, b + list[key])

我不知道如何将键list['je']更改为list['j''],以便成功地输出2。你知道吗


Tags: key代码inouilnouslistenter
2条回答

如果在j'左右使用双引号,即"j'",则可以使用双引号。另外,我建议不要在字典中使用名称list,因为1)它是字典,而不是列表,2)应该避免在变量中使用内置的python名称。你知道吗

而且,看起来共轭处理方式不同,开头是“j”,结尾是“e”(而不是“er”)。你知道吗

dictionary = {"je":"j'","tu":'es',"il ou elle":'e',"nous": 
                  'ons',"vous":'ez',"ils ou elles":'ent'}

veb = input("")

for key in dictionary:
    if veb.endswith('er'):
        b = veb[:-2]
        if key == 'je':
            print(key, dictionary[key] + b + 'e')
        else:
            print(key,b + dictionary[key])

只需将print语句替换为if语句:

if key == "je" and (veb.startswith("a") or veb.startswith("e") or [etc.]):
    print("j'",b + list[key])
else:
    print(key,b + list[key])

相关问题 更多 >