PythonのCurrencyConverter。エラー「金額」が定義されていません、なぜですか?。また、このコードを書くためのより短いまたはよりコンパクトな方法はありますか?

ルカ・ジーバンジー
from currency_converter import CurrencyConverter

エラーを修正するために金額をグローバル化しようとしましたが、役に立ちませんでした

global amounts

c = CurrencyConverter()

currency = input('What ?')
currency = currency.split()

選択する通貨のリスト、辞書

currencys = {"pounds":"GBP" , "dollars":"USD" , "euros":"EUR"}
WCurrency = ' '

これは、文が何であるかを認識する場所です

if 'what' in currency:
    next_word = currency[currency.index('what') + 1]
    if next_word == 'is':

可能であれば短縮したいステートメントの場合は長い

        if 'pounds' in currency:
                amounts = currency[currency.index('pounds') - 1]
                WCurrency = currencys['pounds']
        if 'euros' in currency:
                amounts = currency[currency.index('euros') - 1]
                WCurrency = currencys['euros']

        if 'dollars' in currency:
                amounts = currency[currency.index('dollars') - 1]
                WCurrency = currencys['dollars']
        if 'in' in currency:
                next_word = currency[currency.index('in') + 1]
        if next_word == 'dollars':
                W2Currency = currencys['dollars']
        if next_word == 'pounds':
                W2Currency = currencys['pounds']
        if next_word == 'euros':
                W2Currency = currencys['euros']

これにより、別の形式で文を入力できます

if 'how' in currency:
    next_word = currency[currency.index('how') + 1]
    if next_word == 'many':

可能であれば短縮したいステートメントの場合は長い

        if 'pounds' in currency:
            W2Currency = currencys['pounds']
        if 'euros' in currency:
            W2Currency = currencys['euros']
        if 'dollars' in currency:
            W2Currency = currencys['dollars']
        if 'is' in currency:
            next_word = currency[currency.index('is') + 1]
        if next_word == 'dollars':
            amounts = currency[currency.index('dollars') - 1]
            WCurrency = currencys['dollars']
        if next_word == 'pounds':
            amounts = currency[currency.index('pounds') - 1]
            WCurrency = currencys['pounds']
        if next_word == 'euros':
            amounts = currency[currency.index('euros') - 1]
            WCurrency = currencys['euros']








def convertCurrency(amount, currency, targertCurrency):

    try:
        return c.convert(amount , currency , targertCurrency)
    except:
        print('Invalid currency or amounts!')


print(convertCurrency(amounts, str(WCurrency), str(W2Currency)))
user9011445

elifこれを使用すると、ドルが再び認識されるのを防ぎます。また、それは良い習慣です。すなわち。

    if 'pounds' in currency:
            amounts = currency[currency.index('pounds') - 1]
            WCurrency = currencys['pounds']
    elif 'euros' in currency:
            amounts = currency[currency.index('euros') - 1]
            WCurrency = currencys['euros']

    elif 'dollars' in currency:
            amounts = currency[currency.index('dollars') - 1]
            WCurrency = currencys['dollars']
    elif 'in' in currency:
            next_word = currency[currency.index('in') + 1]
    elif next_word == 'dollars':
            W2Currency = currencys['dollars']
    elif next_word == 'pounds':
            W2Currency = currencys['pounds']
    elif next_word == 'euros':
            W2Currency = currencys['euros']

これは、dollarspoundsがセンテンスにある場合、最初のものを認識することを意味します。残念ながら、このコードは非常に面倒なので、forループを使用することをお勧めします。すなわち。

for word in sentance:
    if word is in [list, of, currencies] and isdigit(sentance(word.index() - 1)):
        currency = word

等々...

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ