2016-09-23 23 views
0

所以,我有一個國家名稱和相應的股票指數字典。要求用戶輸入五個不同的國家,該程序將採用五個相應的股票指數,並對數據進行一些操作。如何在密鑰不在字典中時使用異常處理來創建for循環?

當被問及輸入時,我檢查字典是否可以找到國家,這會發生五次。現在,處理例外的部分並不是我所希望的。我的循環和/或異常處理有什麼問題?

ex_dict = {'United States': '^GSPC','United States of America': '^GSPC', 'Usa': '^GSPC', 'Argentina': '^MERV'} #shortened on purpose 

countries = [] 
print('Please choose five stock exchanges to analyse,') 
print('just name the corresponding countries \n') 

for i in range(0, 5): 
    while True: 
     try: 
      countr = input('Please enter country no. %d: ' %(i+1)) 
      countr = countr.title()    
      if countr in ex_dict.keys(): 
       print('Found the corresponding stock index! \n') 
       countries.append(countr) 
      break 
     except KeyError: 
      print('Country not found, please try again! \n') 
+0

你希望它做什麼,它做什麼呢? –

+2

你不需要捕捉KeyError,因爲你不會遇到一個。在ex_dict中也可以使用'如果countr in ex_dict'而不是'如果countr in ex_dict。keys()' – sisanared

+0

簡單的else語句而不是捕獲KeyError會解決您的問題 – armak

回答

2

此處不會有KeyError,因爲您的代碼永不疲勞訪問字典,只需檢查密鑰是否在keys。你可以簡單地這樣做,以達到同樣的邏輯:

ex_dict = {'United States': '^GSPC','United States of America': '^GSPC', 'Usa': '^GSPC', 'Argentina': '^MERV'} #shortened on purpose 

countries = [] 
print('Please choose five stock exchanges to analyse,') 
print('just name the corresponding countries \n') 

for i in range(0, 5): 
    while True: 
     countr = input('Please enter country no. %d: ' %(i+1)) 
     countr = countr.title() 
     if countr in ex_dict.keys(): 
      print('Found the corresponding stock index! \n') 
      countries.append(countr) 
      break 
     else: 
      print('Country not found, please try again! \n') 

採樣運行:

Please choose five stock exchanges to analyse, 
just name the corresponding countries 

Please enter country no. 1: sdf 
Country not found, please try again! 

Please enter country no. 1: USA 
Found the corresponding stock index! 

Please enter country no. 2: Aregtng 
Country not found, please try again! 

Please enter country no. 2: Argentina 
Found the corresponding stock index! 

Please enter country no. 3: United States 
Found the corresponding stock index! 

Please enter country no. 4: usa 
Found the corresponding stock index! 

Please enter country no. 5: usa 
Found the corresponding stock index! 

注:.keys()是矯枉過正:檢查的關鍵是在一本字典,你只需要k in some_dict

+0

像魅力一樣工作,謝謝很多! – cJc

0

你的休息不在if範圍內......所以它會在第一次嘗試時破壞。

+0

您是否嘗試過代碼?它循環通過罰款和沒有錯誤。 – cJc

+0

我的意思是說,一個有保證開火的休息循環是沒有意義的。但我承認這並沒有回答你關於捕獲異常的問題......正如其他人所說的那樣,當在ex_dict.keys()中嘗試'countr'時,代碼沒有引發任何KeyError'' –

0

doc

Python會引發KeyError異常每當請求dict()對象(使用格式a = adict [key])並且該鍵不在字典中。

在您的一段代碼中,如果您希望在用戶插入國家字典中不存在的鍵時輸出消息,則可以簡單地添加else語句而不是catch語句。

您可以用這種方式更改代碼:

if countr in ex_dict.keys(): 
    print('Found the corresponding stock index! \n') 
    countries.append(countr) 
    break 
else: 
    print('Country not found, please try again! \n') 
0

幾點:

  • 你的代碼永遠不會打的異常,因爲要檢查使用if key in dict.keys()
  • 而且鍵存在,有太多的循環。我認爲for i in range(0,5)已經足夠了。將較簡單

    ex_dict = {'United States': '^GSPC','United States of America': '^GSPC', 'Usa': '^GSPC', 'Argentina': '^MERV'} #shortened on purpose 
    
    countries = [] 
    print('Please choose five stock exchanges to analyse,') 
    print('just name the corresponding countries \n') 
    
    for i in range(0, 5): 
        countr = raw_input('Please enter country no. %d: ' %(i+1)) 
        countr = countr.title() 
        if countr in ex_dict.keys(): 
         print('Found the corresponding stock index! \n') 
         countries.append(countr) 
        else: 
         print('Country not found, please try again! \n') 
    

輸出:

C:\Users\dinesh_pundkar\Desktop>python c.py 
Please choose five stock exchanges to analyse, 
just name the corresponding countries 

Please enter country no. 1: USA 
Found the corresponding stock index! 

Please enter country no. 2: asd 
Country not found, please try again! 

Please enter country no. 3: asd 
Country not found, please try again! 

Please enter country no. 4: USA 
Found the corresponding stock index! 

Please enter country no. 5: United states 
Found the corresponding stock index! 


C:\Users\dinesh_pundkar\Desktop> 
0

我假設你希望它給你一個消息,如果股票是不是在字典ex_dict,我已經修改了代碼,做相同。

ex_dict = {'United States': '^GSPC','United States of America': '^GSPC', 'Usa': '^GSPC', 'Argentina': '^MERV'} #shortened on purpose 

countries = [] 
print('Please choose five stock exchanges to analyse,') 
print('just name the corresponding countries \n') 

for i in range(0, 5): 
    while True: 
     try: 
      countr = input('Please enter country no. %d: ' %(i+1)) 
      countr = countr.title()    
      if countr not in ex_dict.keys(): 
       print ('Try again!') 
      else: 
       print('Found the corresponding stock index! \n') 
       countries.append(countr) 
相關問題