如何将代码从一个.py文件更改为另一个.py文件

时间:2019-03-31 17:23:06

标签: python-3.x

我需要从另一个.py文件的.py文件中添加一些代码行

这是我要添加代码行的文件:

Conversor_monedas

def menu():

    x = int(input("Escoge tu moneda original, siendo:\n1.Dólares\n2.Euros\n3.Soles\n4.Reales\n5.Pesos uruguayos\n6.Pesos Chilenos\n")) #(eeuu,europa,peru,brasil,uruguay,chile)

    y = int(input("Escoge tu moneda convertida, siendo:\n1.Dólares\n2.Euros\n3.Soles\n4.Reales\n5.Pesos uruguayos\n6.Pesos Chilenos\n"))

    valorx = float(input("Cantidad a convertir: "))

    if x == 1:
        dolares(valorx,y)
    if x == 2:
        euros(valorx,y)
    if x == 3:
        soles(valorx,y)
    if x == 4:
        reales(valorx,y)
    if x == 5:
        pesos_uruguayos(valorx,y)
    if x == 6:
        pesos_chilenos(valorx,y)




def dolares(valorx,y):

    if y == 2:
        y = "euros"
        valory = valorx*0.89
    elif y == 3:
        y = "soles"
        valory = valorx*3.32
    elif y == 4:
        y = "reales"
        valory = valorx*3.92
    elif y == 5:
        y = "pesos uruguayos"
        valory = valorx*33.81
    elif y == 6:
        y = "pesos chilenos"
        valory = valorx*680.50

    print(valorx , "dólares equivalen a ", valory , y)


def euros(valorx,y):

    if y == 1:
        y = "dólares"
        valory = valorx*1.12
    elif y == 3:
        y = "soles"
        valory = valorx*3.73
    elif y == 4:
        y = "reales"
        valory = valorx*4.41
    elif y == 5:
        y = "pesos uruguayos"
        valory = valorx*37.97
    elif y == 6:
        y = "pesos chilenos"
        valory = valorx*764.31

    print(valorx , "euros equivalen a ", valory ,y)

menu()

...等等,还有其他两个功能。

我想添加一个带有新变量的其他函数,该变量定义来自不同.py程序的另一个转换变量,该程序询问我要在Conversor_monedas中包括什么函数。

1 个答案:

答案 0 :(得分:0)

因此,这可能与您的想法相去甚远,但是通过在文件中输入转换因子,我相信您可以实现您想要的目标。这是一个工作示例。

#! python3
import csv

global ConvDict

def menu():

    BuildCurrencyDict()

    CurrencyList = []
    FromCurrLit = "Escoge tu moneda original, siendo:"
    ToCurrLit = "Escoge tu moneda original, siendo:"
    # \n1.Dólares\n2.Euros\n3.Soles\n4.Reales\n5.Pesos uruguayos\n6.Pesos Chilenos\n")) #(eeuu,europa,peru,brasil,uruguay,chile
    for i, Currency in enumerate(ConvDict):
        FromCurrLit = FromCurrLit + "\n" + str(i+1) + ". " + Currency
        ToCurrLit = ToCurrLit + "\n" + str(i+1) +  ". " + Currency
        CurrencyList.append(Currency)

    FromCurrLit = FromCurrLit + "\n"
    ToCurrLit = ToCurrLit + "\n"
    x = int(input(FromCurrLit)) #(eeuu,europa,peru,brasil,uruguay,chile)

    y = int(input(ToCurrLit))

    valorx = float(input("Cantidad a convertir: "))

    ConvFactor = float(ConvDict[CurrencyList[x-1]][CurrencyList[y-1]])
    valory = valorx * ConvFactor

    print(valorx , CurrencyList[x-1], "equivalen a", valory , CurrencyList[y-1])


def BuildCurrencyDict():
    global ConvDict

    CurrTypes= []
    ConvDict = {}
    # Put a matrix of currencies in CurrConv.txt
    with open('CurrConv.txt') as csvfile:
        data = csv.reader(csvfile, delimiter="\t")
        for LineIn in data:
            # user_info[name] = {'code': code, 'name': name, 'id': id}
            for i, Item in enumerate(LineIn):
                if data.line_num == 1:
                    if i == 0:
                        continue
                    else:
                        CurrTypes.append(Item)
                else:
                    if i == 0:
                        FromCurr = Item
                    else:
                        if i == 1:
                            ConvDict[FromCurr] = {CurrTypes[i-1]:Item}
                        else:
                            ConvDict[FromCurr][CurrTypes[i-1]] = Item

    # print(ConvDict["Euro"]["Dollar"]) # Testing Code

if __name__ == '__main__':
    menu()

CurrConv.txt看起来像这样。我使用制表符定界,但如果需要,可以使用分号或其他定界符。第1行以制表符开头。然后在值之间有制表符。只需将其他货币添加到文件中,它们就会被添加到您的转换提示中。

    Euro    Dollar  Yen
Euro    1   1.12    124.53
Dollar  0.89    1   110.88
Yen 0.008   0.009   1