我正在尝试在python中存储列表,然后与用户输入匹配

时间:2014-04-20 13:43:59

标签: python

我试图在python中存储一些货币。然后,如果用户输入的货币来自行中的第一件货币和用户输入的货币以匹配行中的第二件物品然后使用汇率(列表中的第三件事)

               exchange_rate=(("pound", "euro", "1.21"),
                               ("euro", "pound", "0.82"),
                               ("pound", "dollar", "1.68"),
                               ("dollar", "pound", "0.59"),
                               ("pound", "yen", "171.71"),
                               ("yen", "pound", "0.0058"),
                               ("euro", "dollar", "1.38"),
                               ("dollar", "euro", "0.72"),
                               ("euro", "yen", "141.45"),
                               ("yen", "euro", "0.0071"),
                               ("dollar", "yen", "102.18")
                               ("yen", "dollar", "0.0098"))

以后的程序:

                for rows in exchange_rate:
                        if currency_from==exchange_rate[0] and currency_to==exchange_rate[1]:
                             exchange=exchange_rate[2]
                             answer=input("Would you like to use this currency:",exchange,"or input your own?")

我将所有用户输入作为字符串,以便更容易与列表中的字符串匹配。 我不想使用csv文件。 它一直给我一个错误:

                 ("yen", "dollar", "0.0098"))
                TypeError: 'tuple' object is not callable

我还没有使用过这个网站,所以我真的不知道如何以及如果我被允许给予最佳答案奖励(但如果你让我知道我将如何做到这一点)< / p>

我也不是蟒蛇新手,但我也不太了解。 我感谢我能得到的任何帮助/建议。 感谢您抽出宝贵时间阅读本文。

           def rates():
               global exchange
               global exchange_rate
               exchange_rate = {("pound", "euro"): 1.21,
                 ("euro", "pound"): 0.82,
                 ("pound", "dollar"): 1.68,
                 ("dollar", "pound"): 0.59,
                 ("pound", "yen"): 171.71,
                 ("yen", "pound"): 0.0058,
                 ("euro", "dollar"): 1.38,
                 ("dollar", "euro"): 0.72,
                 ("euro", "yen"): 141.45,
                 ("yen", "euro"): 0.0071,
                 ("dollar", "yen"): 102.18,
                 ("yen", "dollar"): 0.0098}
exchange = exchange_rate[currency_from, currency_to]
def amount():
    global amount
    amount=str(input("What is the amount you wish to convert?: "))

def currency_from():
    global currency_from
    currency_from=str(input("Please type in your chosen currency: "))

def currency_to():
    global currency_to
    currency_to=str(input("Please type in your chosen currency: "))

def exchangerate():
    global answer
    global rate
    global total
    global rates
    global total1
    found=False
    count=0
    while count ==0 and not found:
       if currency_from!= exchange_rate[currency_from] and currency_to !=exchange_rate[currency_to]:
            count=count+1
     else:
            found=True
            currency_from== exchange_rate[currency_from] and currency_to==exchange_rate[currency_to]
            rate=exchange_rate[2]

answer=str(input("Would you like to use this xchange rate :",rate))

if answer== "yes":
    total=amount*rate
    print(amount, currency_from, "is:",total,currency_to)
else:
    rates=float(input("Enter rate: "))
    total1=amount*rates
    print(amount, currency_from, "is:",total1,currency_to)
#main
print("Welcome! This program is only viable for the following    currencies:dollar,euro,yen,pound")
amount()
currency_from()
currency_to()
rates()
exchangerate()

请注意,程序中的缩进是正确的,但是当我将其复制到此处时,它可能已经更改。

2 个答案:

答案 0 :(得分:2)

你有一个简单的印刷错误。您的元组元组中的第二个元组的最后一个元组之后缺少,:{/ 1}}:

这是正确的版本:

exchange_rate

答案 1 :(得分:1)

exchange_rate成为dict,而不是元组:

exchange_rate = {("pound", "euro"): 1.21,
                  ("euro", "pound"): 0.82,
                  ("pound", "dollar"): 1.68,
                  ("dollar", "pound"): 0.59,
                  ("pound", "yen"): 171.71,
                  ("yen", "pound"): 0.0058,
                  ("euro", "dollar"): 1.38,
                  ("dollar", "euro"): 0.72,
                  ("euro", "yen"): 141.45,
                  ("yen", "euro"): 0.0071,
                  ("dollar", "yen"): 102.18,
                  ("yen", "dollar"): 0.0098}

然后你可以删除for-loop并更改:

if currency_from==exchange_rate[0] and currency_to==exchange_rate[1]:
     exchange=exchange_rate[2]

exchange = exchange_rate[currency_from, currency_to]

当项目的订单很重要时,请使用元组或列表。如果要根据键查找值,请使用dict。


您可以更改许多内容,例如删除全局语句以及使用函数调用中的返回值。但在这里,我将重点关注使用dict运行代码所需的两项更改:

def rates():
    global exchange
    global exchange_rate
    exchange_rate = {
        ("pound", "euro"): 1.21,
        ("euro", "pound"): 0.82,
        ("pound", "dollar"): 1.68,
        ("dollar", "pound"): 0.59,
        ("pound", "yen"): 171.71,
        ("yen", "pound"): 0.0058,
        ("euro", "dollar"): 1.38,
        ("dollar", "euro"): 0.72,
        ("euro", "yen"): 141.45,
        ("yen", "euro"): 0.0071,
        ("dollar", "yen"): 102.18,
        ("yen", "dollar"): 0.0098}

def amount():
    global amount
    amount = float(input("What is the amount you wish to convert?: "))  # 2

def currency_from():
    global currency_from
    currency_from = str(input("Please type in your chosen currency: "))

def currency_to():
    global currency_to
    currency_to = str(input("Please type in your chosen currency: "))

def exchangerate():
    global answer
    global rate
    global total
    global rates
    global total1
    found = False
    rate = exchange_rate[currency_from, currency_to]       # 1
    answer = str(
        input("Would you like to use this xchange rate : {}".format(rate)))

    if answer == "yes":
        total = amount * rate
        print(amount, currency_from, "is:", total, currency_to)
    else:
        rates = float(input("Enter rate: "))
        total1 = amount * rates
        print(amount, currency_from, "is:", total1, currency_to)

# main
print("Welcome! This program is only viable for the following    currencies:dollar,euro,yen,pound")
amount()
currency_from()
currency_to()
rates()
exchangerate()
  1. dict的目的是让你查询汇率 不使用while-loop
  2. 为方便数学运算,金额应为浮点数或小数,而不是a 海峡