2017-04-24 32 views
-2

予期せぬコードのインデントが発生していますが、コードが正しくインデントされていてもエラーが発生しています。私は1つを除いて、複数のと試みてきたし、私はまだエラーを取得:そう予期せぬ機能の中抜き

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

あなたはどのくぼみを参照していますか?あなたのコード?あなたの出力?あなたの質問をより明確に編集してください。あなたのプログラムからの出力を表示して問題を説明します。また、あなたのタイトルに予期しない "インデントされていない"と記載されていますが、予期せぬ「インデント」が記載されています。 *注:私は他の質問を検索する言及を削除しました。関係ありません。メモ:コメントは左詰めで段落見出しとして解釈されるため、コードを再フォーマットする必要がありました* –

+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() 
関連する問題