1

我开发了一套简单的基于多语言控制台的计算器,我的问题是,在第一次执行时,当用户选择他的语言时,他将获得两倍于软件标题的信息。

------------------------------------------------------
Trooper Math Suite - ver. 0.1.5.1
------------------------------------------------------
Choose your default language:
------------------------------------------------------
1. English
2. Português Brasileiro
3. Español
------------------------------------------------------
You: 1
------------------------------------------------------
Trooper Math Suite - ver. 0.1.5.1
------------------------------------------------------
Select the calculator:
------------------------------------------------------
1. Biquadratic Equations Calculator
2. Pythagorean Theorem Calculator
------------------------------------------------------
You: 

启动器的代码是:

# tms.py - launcher

ver = "0.1.5.1"
sep = "-" * 54

print(sep)
print("Trooper Math Suite - ver. {0}".format(ver))
print(sep)

try:
    from config import *

    if default_lang == "1":
        print("""Select the calculator:\n{0}
1. Biquadratic Equations Calculator
2. Pythagorean Theorem Calculator\n{0}""".format(sep))
        go = input("You: ")
        print(sep)
        if go == "1": import bc
        elif go =="2": import pc

    if default_lang == "2":
        print("""Selecione a calculadora:\n{0}
1. Calculadora de Equações Biquadradas
2. Calculadora do Teorema de Pitágoras\n{0}""".format(sep))
        go = input("You: ")
        print(sep)
        if go == "1": import bc
        elif go =="2": import pc

    if default_lang == "3":
        print("""Seleccione la calculadora:\n{0}
1. Calculadora de Ecuaciones Bicuadráticas
2. Calculadora de Teorema de Pitágoras\n{0}""".format(sep))
        go = input("You: ")
        print(sep)
        if go == "1": import bc
        elif go =="2": import pc

except:
    print("""Choose your default language:\n{0}
1. English
2. Português Brasileiro
3. Español\n{0}""".format(sep))
    language=input("You: ")

    while language != "1" and language != "2" and language != "3":
        print(sep)
        print("You must choose between 1, 2 or 3.")
        print(sep)
        language=input("You: ")

    file = open("config.py", "w")
    file.write('default_lang = "{0}"\n'.format(language))
    file.close()
    import tms

我用谷歌搜索过,人们建议使用 def lang() 之类的函数,但我不能这样做,因为:

SyntaxError: import * only allowed at module level
4

1 回答 1

3

最好的解决方案是根本不使用import *。相反,保持模块命名空间不变,只在需要的地方使用import configconfig.some_object

可以说,更好的方法是为您的配置文件使用另一种格式,例如JSON。这将使使用配置文件更容易,并避免恶意配置文件的可能性。

于 2013-06-27T17:26:29.013 回答