2017-04-24 66 views
-2

我得到一個意外的unindent這個函數,我仍然收到錯誤,即使代碼正確縮進。我試圖用一個不同之處,並多次和我仍然得到錯誤:意外unindent函數

def showHand(): 

    print('This game will let you open your saved Dream Hand.') 

    #getting the file name the user wants to open 
    filename = input('Please enter your dream hand file name: ') 

    try: 
     #opening 
     infile = open(filename, 'r') 

     #reading the values inline 
     card1 = int(infile.readline()) 
     card2 = int(infile.readline()) 
     card3 = int(infile.readline()) 
     card4 = int(infile.readline()) 
     card5 = int(infile.readline()) 

     #closing file 
     infile.close() 

     #printing values through face value 
     print('Your Dream Hand is: ') 
     faceValue(card1) 
     faceValue(card2) 
     faceValue(card3) 
     faceValue(card4) 
     faceValue(card5) 

    except IOError: 
     print('An error has occurred') 

    finally: 
     print('Thank you for playing') 
+0

你指的是什麼縮進?你的代碼?你的輸出?請編輯您的問題,以便更清楚。並顯示你的程序輸出來說明問題。此外,您的標題提到了一個意想不到的「unindent」,但是您的描述中提到了意想不到的「縮進」。 *注:我刪除了提及搜索其他問題;不相關。另外請注意:我必須重新格式化您的代碼,因爲您的註釋是左對齊的,並被解釋爲段落標題。* –

+0

當我嘗試運行完整程序時,我收到了一個意想不到的錯誤消息,它指的是第一行def showHand(): –

+0

@DavidMakogon爲什麼用'IndentError'編輯代碼? –

回答

0

縮進下def showHand():

def showHand(): 

    print('This game will let you open your saved Dream Hand.') 

    #getting the file name the user wants to open 
    filename = input('Please enter your dream hand file name: ') 

    try: 
     #opening 
     infile = open(filename, 'r') 

     #reading the values inline 
     card1 = int(infile.readline()) 
     card2 = int(infile.readline()) 
     card3 = int(infile.readline()) 
     card4 = int(infile.readline()) 
     card5 = int(infile.readline()) 

     #closing file 
     infile.close() 

     #printing values through face value 
     print('Your Dream Hand is: ') 
     faceValue(card1) 
     faceValue(card2) 
     faceValue(card3) 
     faceValue(card4) 
     faceValue(card5) 

    except IOError: 
     print('An error has occurred') 

    finally: 
     print('Thank you for playing') 
0

好代碼,我複製你的代碼,在這裏,它運行,所以這是一個重構。

def faceValue(card): 
    if card == 1: 
     return 'A' 
    if card > 10: 
     return {11: 'J', 12: 'Q', 13: 'K'}[card] 
    else: 
     return card 

def showHand(): 

    print('This game will let you open your saved Dream Hand.') 

    #getting the file name the user wants to open 
    filename = input('Please enter your dream hand file name: ') 

    try: 
     #opening 
     with open(filename) as infile: 
      cards = [int(infile.readline()) for _ in range(5)] 

     #printing values through face value 
     print('Your Dream Hand is: ') 
     for card in cards: 
      print(faceValue(card)) 

    except IOError: 
     print('An error has occurred') 

    finally: 
     print('Thank you for playing') 

showHand()