2014-09-19 59 views
1

我正在寫一個程序,加載一個文件中的數據列表,我需要該程序來區分該行中的數據是字符串還是整數。然而,在我所做的代碼中,程序不會區分數字和字符串。Python:如何在if語句中使用類型函數?

數據列表中的一個例子,我有:

HAJOS 
ALFRED 
1896 
1 

我的代碼:

def medalsYear(): 
    times = 1 
    totalGold = 0 
    totalSilver = 0 
    totalBronze = 0 
    while times <= 5: 
    alpha = fob.readline() #reads file line by line# 
    print(alpha) 
    times = times + 1 
    if type(alpha) == int: 
     if alpha == 1: 
      totalGold = totalGold + 1 
      print("gold medal won") 
     elif alpha == 2: 
      totalSilver = totalSilver + 1 
      print("silver medal won") 
     elif alpha == 3: 
      totalBronze = totalBronze + 1 
      print("bronze medal won") 
     else: 
      pass 
    else: 
     print('is a string') 
    print(totalGold, "Gold medals won") 
    print(totalSilver, "Silver medals won") 
    print(totalBronze, "Bronze medals won") 

我的問題是,當程序讀取具有整線,它不如果該行包含整數並從那裏遍歷相應的if語句,則可以正確確定。目前我的輸出看起來像這樣。

HAJOS 
is a string 
ALFRED 
is a string 
1896 
is a string 
1 
is a string 

is a string 
0 Gold medals won 
0 Silver medals won 
0 Bronze medals won 
done 
+1

作爲你的輸出顯示,所有的值*其實都是*字符串。當你從這樣的文件讀取數據時,你總是閱讀字符串。如果你想將它們轉換爲整數,你需要自己做。 – BrenBarn 2014-09-19 19:02:57

+1

提示:'int(alpha)'會將'alpha'轉換爲一個整數,如果不能則拋出'ValueError'。 – 2014-09-19 19:04:23

+0

'如果alpha.isdigit()'也可以工作 – 2014-09-19 19:18:59

回答

2

數據從文件中讀取是總是將是一個字符串。你需要嘗試和轉換這些線路,沒有測試他們的類型:

try: 
    alpha = int(alpha) 
    if alpha == 1: 
     totalGold = totalGold + 1 
     print("gold medal won") 
    elif alpha == 2: 
     totalSilver = totalSilver + 1 
     print("silver medal won") 
    elif alpha == 3: 
     totalBronze = totalBronze + 1 
     print("bronze medal won") 
except ValueError: 
    print('is a string') 

int()將引發ValueErroralpha不能被解釋爲一個整數。如果引發異常,Python會跳轉到except ValueError:塊,而不是執行try:套件的其餘部分。

+0

ahh比我的(現在刪除的)答案好得多...閱讀問題的方式...:P +1 – 2014-09-19 19:04:52

+0

@JoranBeasley:將問題編輯成某種形狀的外形有助於理解。 :-) – 2014-09-19 19:05:31

+0

我只是試圖將其實現到我的代碼中,它大部分工作。唯一的問題就是在使用它的時候,它並不會更新任何情況下贏得的獎牌總數。它確實正確地將字符串轉換爲整數。 編輯:沒關係,我的部分只是語法錯誤。 – SirGoose 2014-09-19 19:48:26

0

您可以製作一個字典,其中的鍵是"1","2" and "3"以對應於金,銀,銅牌並使用dict.get。

with open(infile) as f: 
    times = 1 
    medal_dict = {"1": 0, "2": 0, "3": 0} 
    while times <= 5: 
     alpha = f.readline().rstrip() #reads file line by line# 
     times += 1 
     if medal_dict.get(alpha) is not None: 
      medal_dict[alpha] += 1 
     else: 
      print("is a string") 
    print(medal_dict["1"], "Gold medals won") 
    print(medal_dict["2"], "Silver medals won") 
    print(medal_dict["3"], "Bronze medals won") 

,輸出:

(1, 'Gold medals won') 
(0, 'Silver medals won') 
(0, 'Bronze medals won') 

如果你想打印時,獎牌通過循環榮獲:

with open(infile) as f: 
    times = 1 
    medal_dict = {"1": [0,"gold"], "2": [0,"silver"], "3": [0,"bronze"]} 
    while times <= 5: 
     alpha = f.readline().rstrip() #reads file line by line# 
     print(alpha) 
     times += 1# 
     check = medal_dict.get(alpha) 
     if check is not None: 
      medal_dict[alpha][0] += 1 
      print("{} medal won".format(check[1])) 
     else: 
      print("is a string") 
    print(medal_dict["1"][0], "Gold medals won") 
    print(medal_dict["2"][0], "Silver medals won") 
    print(medal_dict["3"][0], "Bronze medals won")