2017-12-02 49 views
-3
from currency_converter import CurrencyConverter 

我試圖globalling達修復我的錯誤,但它並沒有幫助貨幣的的Python中的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語句我想是笑rtened如果可能的話

 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))) 
+0

這將是有益的,如果你解釋一下你的程序正在試圖做的事。我知道這似乎很明顯,但對我們來說並不明顯。考慮一下:如果你的代碼能夠工作,我們可以閱讀它並找出它的功能。但是,您向我們展示的代碼不符合您的要求,所以沒有多少數據可以告訴我們應該做什麼,因爲它沒有這樣做。 – BoarGules

+0

您的權利對不起,您應該輸入一個字符串,例如:'什麼是10磅美元',它將確定金額:'10'基礎貨幣:'磅'和您試圖找出的貨幣:''美元。 –

+0

那麼如果你說'10美元多少英鎊'它應該確定相同。 –

回答

0

使用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 

等等...

相關問題